PHP序列化ajax数组不工作

时间:2022-03-09 21:36:47

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

我在PHP外观中创建了以下HTML字段

<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

我试图通过单击此按钮调用jquery函数来保存这些字段中的数据

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.

这是被调用的脚本。我知道正在调用js,因为“alert(”now“)”正在弹出,但dataString没有被正确填充。我在http://jsfiddle.net/上测试了它,它工作正常,但不能在我的网站上运行。

<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>

1 个解决方案

#1


2  

Try this with the help of FormID like this:

在FormID的帮助下尝试这样:

<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;
    });

#1


2  

Try this with the help of FormID like this:

在FormID的帮助下尝试这样:

<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;
    });