0

I'm using jQuery and Codeigniter to update the database via AJAX. The following code does not seem to do anything or send anything to my controller when the button is clicked...

jQuery:

$("#button_submit1").click(function(e) { $.ajax({ type: "POST", url: window.location.href, dataType: "json", data: $('#writereview_form').serialize() + '&ajax=true', cache: false, success: function(data) { alert("yay"); //$("#writereview_box").fadeOut(1000); } }); return false; }); 

HTML:

<form action="http://mysite.com/places/writereview/2107" id="writereview_form" method="post" accept-charset="utf-8"> ... ... <input type="submit" name="submit" value="Submit Review" class="button_submit" id="button_submit1"> 

Any ideas why the ajax data is not being sent?

5
  • Have you checked that there are no JavaScript errors in the page? Commented Jul 21, 2011 at 16:12
  • Yup no javascript errors using Chrome's console. Nothing happens when I click the submit button Commented Jul 21, 2011 at 16:12
  • 1
    because your already submiting it with your input type submit button. trying adding a plan link with a button style and putting onclick="Functin" Commented Jul 21, 2011 at 16:14
  • 1
    Try to change type="submit" to type="button" Commented Jul 21, 2011 at 16:17
  • 1
    @RPM: The return false will prevent the default submit. My suspect would be window.location.href. Do you see the XHR request in chrome's dev tools? Commented Jul 21, 2011 at 16:17

2 Answers 2

4

In this case its better to bind to the 'submit' event on the form and then use preventDefault() to stop the HTML submission and use Ajax instead.

You should also use the form's action instead of window.location

$("#writereview_form").submit(function(e) { var $this = $(this); e.preventDefault(); $.ajax({ type: "POST", url: $this.attr('action'), dataType: "json", data: $this.serialize() + '&ajax=true', cache: false, success: function(data) { alert("yay"); //$("#writereview_box").fadeOut(1000); } }); }); 
Sign up to request clarification or add additional context in comments.

Comments

0

Try withiut using window.location.href and giving url as url: "page.php",

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.