1

The follwing ajax script isn't sending data to php, the page just reloads & form input values are passed onto the url.

Script

<script>
  $("#addProducts").submit(function(event) {
     var str = $("addProducts").serialize();
     event.preventDefault();
     $.ajax({
        type: "POST",
        url: "subAddProduct.php",
        data:str
     })
  });
</script>

HTML Form

<form enctype="multipart/form-data" id="addProducts">
...
</form>
Steffan
  • 536
  • 5
  • 15
Jaskaran S.P
  • 1,055
  • 2
  • 14
  • 15

10 Answers10

2

There's already a problem in your code: $("addProducts").serialize(); should be $("#addProducts").serialize();.

I just ran some tests. The problem is because you try to bind your function before your document is ready. Please replace your code by the code below:

$(document).ready(function() {
    $("#form1").submit(function(event) {
         var str = $("#form1").serialize();
         event.preventDefault();
         $.ajax({
            type: "POST",
            url: "test.php",
            data: str
        });
    });
});

About what Zeeshan Bilal and pvorb said, I'm afraid it's false. submit() is the right function to use (see jQuery documentation).

Description: Bind an event handler to the "submit" JavaScript event, or trigger that event on an element.

PLPeeters
  • 1,009
  • 12
  • 26
1
$("#addProducts").click(function(event) {
    var str = $("#addProducts").serialize();
    event.preventDefault();
    $.ajax({
        type: "POST",
        url: "subAddProduct.php",
        data:str
    });
});
underscore
  • 6,495
  • 6
  • 39
  • 78
1

Perhaps you are trying to bind your function when document is not ready.

$(document).ready(function() {
 $("#addProducts").submit(function(event) {
 var str = $("addProducts").serialize();
 event.preventDefault();
 $.ajax({
 type: "POST",
 url: "subAddProduct.php",
 data:str
   })});

});

Mykyta Karpyshyn
  • 198
  • 2
  • 13
1

Its not ajax issue, actually you are using $("#addProducts").submit that send a page submit request and cause page reload. Use click instead of submit.

The another mistake $("addProducts").serialize(), add # for id selector. Below is the sample code:

$("#addProducts").click(function(event) {
    var str = $("#addProducts").serialize();
    event.preventDefault();
    $.ajax({
        type: "POST",
        url: "subAddProduct.php",
        data:str
    });
});
Zeeshan Bilal
  • 1,147
  • 1
  • 8
  • 22
1

Adjust your JS as below

<script>
$("#addProducts").click(function(event) {
    $.ajax({
        type: "POST",
        url: "subAddProduct.php",
        dataType : 'json', //data type can be any type like json, html etc.
        data:'str='+str
        success : function(data) {
            //perform your success process here
        }
    });
});
</script>

I have not tested the above code, but it should work, as i use same codes for my ajax features.

Also check jquery docs for $.ajax http://api.jquery.com/jQuery.ajax/

Joe
  • 15,205
  • 8
  • 49
  • 56
Altaf Hussain
  • 5,166
  • 4
  • 30
  • 47
1
<script>
  $("#addProducts").submit(function(event) {
     event.preventDefault();
     var str = $("#addProducts").serialize();
     $.ajax({
        type: "POST",
        url: "subAddProduct.php",
        data:str
     })
  });
</script>

HTML Form

<form enctype="multipart/form-data" id="addProducts" action="">
...
<input type="submit" name="submit" value="submit">
</form>
Amir
  • 4,089
  • 4
  • 16
  • 28
1

To solve this problem you should modify your code in this way:

<script>
$("#addProducts").submit(function(event) {
  var str = $("#addProducts").serialize();
  $.ajax({
    type: "POST",
    url: "subAddProduct.php",
    data:str,
    success: function(data){
      //perform your success process here
      return false;
    }
  })
});
</script>
QArea
  • 4,955
  • 1
  • 12
  • 22
1

Try Setting the Ajax async property to false as shown below

<script>
  $("#addProducts").submit(function(event) {
     var str = $("addProducts").serialize();
     event.preventDefault();
     $.ajax({
        async:false
        type: "POST",
        url: "subAddProduct.php",
        data:str
     })
  });
</script>
Ninad
  • 1,871
  • 3
  • 15
  • 23
1

Missing the hashtag when you serialize... your code is having jQuery look for an element called "addProducts" not an element with an id of "addProducts" change this line

var str = $("addProducts").serialize();

to this line

var str = $("#addProducts").serialize();
CDspace
  • 2,639
  • 18
  • 30
  • 36
Banning
  • 2,279
  • 2
  • 16
  • 20
0
<script>
    $("#addProducts").submit(function(event) {
    event.preventDefault();
    var str = $("#addProducts").serialize();
      event.preventDefault();
      $.ajax({
      type: "POST",
      url: "subAddProduct.php",
      data:str
   })
    });
    return false;
</script>

You need to preventDefault, which stops the form being submitted normally, causing the page to reload. You then need to return false after because Firefox doesn't like preventDefault :P

Karl
  • 5,435
  • 11
  • 44
  • 70