SamIAm
SamIAm

Reputation: 47

PHP Serialize on ajax array Not Working

I have the following HTML fields being created inside a PHP look

<td><input type=\"checkbox\" name=\"investigator_id[]\" id=\"investigator_id\" value=\"$name_degree[$i]\"> 
<td><input type=text name=\"inv_rank[]\" id=inv_rank maxlength=\"2\" size=\"2\"></td>
<td><textarea name=\"inv_comm[]\" id=inv_comm rows=2 cols=20></textarea></td>

I am trying to save the data in these fields by calling a jquery function based on clicking on this button

Here is the script that is being called. I know that the js is being called because the "alert("now")" is poping up, but the dataString is not being populated correctly. I tested this on http://jsfiddle.net/ and it worked fine, but won't work on my site.

<script>
$(document).ready(function() {
    $("#submit").click(function() {
        alert("now");
        var dataString = $("'[name=\"investigator_id\[\]\"]', '[name=\"inv_rank\[\]\"]','[name=\"inv_comm\[\]\"]'").serialize();
        alert("ee"+dataString);
        $.ajax({
            type: "POST",
            url: "save_data.php",
            dataType: "json",
            data: dataString,
            success: function(data){
                alert("sure"+data);
                $("#myResponse").html(data);
            },
            error : function(XMLHttpRequest, textStatus, errorThrown) {
                alert("There was an error.");
            }
        });
    });
});
</script>

Upvotes: 3

Views: 119

Answers (1)

devpro
devpro

Reputation: 16117

Try this with the help of FormID like this:

<form method="post" id="yourFromID">
//Your form fields.
</form>

JS Code:

$("#yourFromID").submit(function (e){
  e.preventDefault();
  var dataString = $(this).serialize();

  // then you can do ajax call, like this
  $.ajax({
       url: 'site.com',
       data: dataString,
       methodL 'post',
       success: function(){...}
  })

return false;
    });

Upvotes: 2

Related Questions