Page refresh instead of Ajax Download without

In the submit form, I want to load a div with an updated list of mysql tables. I submit form variables in php and submit them to mysql table. the same page displays the complete table data. I want to load data into a div tag, like a form. Thus, it seems that the information is loading above the form.

My javascript

$("#formSubmit").submit(function(){

    var name = $("input#name").val();
    var comment = $("input#comment").val();
    var filmnumber = $("input#hidden").val();

    var dataString = 'name='+ name + '&comment=' + comment + '&filmnumber=' + filmnumber;

    $.ajax({
        type: "POST",
        url: "comment.php",
        data: dataString,
        success: function() {
            $('#2').load('comment.php');
        }
    });

My form is

<div id="2">                                    
    <p>Add a Comment</p>
    <form id="formSubmit" method="POST">
        <div>
            <input type="hidden" name="hidden" id="hidden" value="2">
            <label for="name">Your Name</label>
            <input type="text" name="name" id="name" />

            <label for="body">Comment Body</label>
            <textarea name="comment" id="comment" cols="20" rows="5"></textarea>

            <input type="submit" id="comment" class="button" value="Submit" />
    </form>
    </div>

All he does is refresh the page and not load information in div 2: S

thanks for the help

+3
source share
2 answers

You need to prohibit form redirection using the preventDefaultevent object method :

$("#formSubmit").submit(function(e){ // add the event object as an argument to your function
    e.preventDefault(); // right here

    var name = $("input#name").val();
    var comment = $("input#comment").val();
    var filmnumber = $("input#hidden").val();

    var dataString = 'name='+ name + '&comment=' + comment + '&filmnumber=' + filmnumber;

    $.ajax({
      type: "POST",
      url: "comment.php",
      data: dataString,
      success: function() {
       $('#2').load('comment.php');
      }
    });
});
+3
source

false , . , preventDefault(); . - , , return false;

$("#formSubmit").submit(function(e){ // add the event object as an argument to your function


var name = $("input#name").val();
var comment = $("input#comment").val();
var filmnumber = $("input#hidden").val();

var dataString = 'name='+ name + '&comment=' + comment + '&filmnumber=' + filmnumber;

$.ajax({
  type: "POST",
  url: "comment.php",
  data: dataString,
  success: function() {
   $('#2').load('comment.php');
  }
});
return false;//right here

});

+1

All Articles