2012-02-29 159 views
18

我想提交一个POST表单,其中包含一个textarea字段和一个输入字段(type =“checkbox”,任意/可变数量的复选框)网站通过jQuery的.ajax()。 PHP接收textarea数据并且ajax响应正确显示给用户。但是,似乎PHP没有收到复选框数据(是否检查过)。我怎样才能使这个工作?下面是我的代码有:通过jQuery将多个复选框数据发送到PHP ajax()

的HTML:

<form method="post" action="myurl.php" id=myForm> 
    <textarea id="myField" type="text" name="myField"></textarea> 
    <input type="checkbox" name="myCheckboxes[]" id="myCheckboxes" value="someValue1" /> 
    <input type="checkbox" name="myCheckboxes[]" id="myCheckboxes" value="someValue2" /> 
    ...(maybe some more checkboxes - dynamically generated as necessary) 
    <input id="submit" type="submit" name="submit" value="Submit" onclick="submitForm()" /> 
</form> 

jQuery的:

function submitForm() { 
$(document).ready(function() { 
$("form#myForm").submit(function() { 

     var myCheckboxes = new Array(); 
     $("input:checked").each(function() { 
      myCheckboxes.push($(this).val()); 
     }); 

     $.ajax({ 
      type: "POST", 
      url: "myurl.php", 
      dataType: 'html', 
      data: { myField:$("textarea[name=myField]").val(), 
        myCheckboxes:myCheckboxes }, 
      success: function(data){ 
       $('#myResponse').html(data) 
      } 
     }); 
     return false; 
}); 
}); 

现在,PHP

$myField = htmlspecialchars($_POST['myField'])); 
if(isset($_POST['myCheckboxes'])) 
{ 
    for ($i=0; $i < count($_POST['myCheckboxes']); $i++) 
    { 
     // do some stuff, save to database, etc. 
    } 
} 
// create the response 
$response = 'an HTML response'; 
$response = stripslashes($response); 
echo($response); 

一切都很正常:当窗体提交新记录存储在我的数据库中,响应被回送到网页,但复选框数据未发送。我想知道哪些复选框已被选中。我已阅读.serialize(),JSON等,但没有一个工作。我必须在jQuery和PHP中序列化/ JSON吗?怎么样?使用复选框发送表单数据时,其中一种方法比另一种更好吗?我已经坚持了2天。任何帮助将不胜感激。提前致谢!

+0

你不必担心表单元素 试试这个 http://stackoverflow.com/questions/19029703/jquery -using-ajax-to-send-data-and-save-in-php/19029778#19029778 – rohitcopyright 2013-09-26 16:17:54

回答

13
var myCheckboxes = new Array(); 
$("input:checked").each(function() { 
    data['myCheckboxes[]'].push($(this).val()); 
}); 

你是推复选框错阵列data['myCheckboxes[]']而不是myCheckboxes.push

+0

谢谢。我改变了这一点,但我仍然没有收到PHP端的数据。我已经尝试了几种不同的方法来处理jQuery的复选框(我发现了这个网站的大部分想法),但是他们都没有工作。我的代码创建了一个mySQL数据库记录,但复选框字段始终为空,即使我正在检查发送的表单中最多8个不同的复选框。 – 2012-02-29 04:39:42

+0

Uncaught ReferenceError:data is not defined – Andy 2016-10-31 11:10:56

+0

@Andy'''myCheckboxes.push($(this).val());''' – Vertisan 2017-06-01 00:31:05

29

是它与jquery.serialize()

HTML漂亮的工作

<form id="myform" class="myform" method="post" name="myform"> 
<textarea id="myField" type="text" name="myField"></textarea> 
<input type="checkbox" name="myCheckboxes[]" id="myCheckboxes" value="someValue1" /> 
<input type="checkbox" name="myCheckboxes[]" id="myCheckboxes" value="someValue2" /> 
<input id="submit" type="submit" name="submit" value="Submit" onclick="return submitForm()" /> 
</form> 
<div id="myResponse"></div> 

JQuery的

function submitForm() { 
var form = document.myform; 

var dataString = $(form).serialize(); 


$.ajax({ 
    type:'POST', 
    url:'myurl.php', 
    data: dataString, 
    success: function(data){ 
     $('#myResponse').html(data); 


    } 
}); 
return false; 
} 

现在的PHP,我出口POST数据

echo var_export($_POST); 

你可以看到所有的复选框值是sent.I希望它可以帮助你

+0

让我知道它是否有效。 – 2012-02-29 04:54:31

+0

感谢您的建议。我得到它的工作! – 2012-02-29 06:21:27

+0

感谢@WinnMinnSoe让我发现.serialize方法! =) – Lucas 2012-07-22 16:16:28

0

你此刻的代码似乎是所有对。使用它检查复选框数组包含的内容。将此代码添加到您的php脚本的顶部,并查看复选框是否传递给您的脚本。

echo '<pre>'.print_r($_POST['myCheckboxes'], true).'</pre>'; 
exit; 
2

检查了这一点。

<script type="text/javascript"> 
    function submitForm() { 
$(document).ready(function() { 
$("form#myForm").submit(function() { 

     var myCheckboxes = new Array(); 
     $("input:checked").each(function() { 
      myCheckboxes.push($(this).val()); 
     }); 

     $.ajax({ 
      type: "POST", 
      url: "myurl.php", 
      dataType: 'html', 
      data: 'myField='+$("textarea[name=myField]").val()+'&myCheckboxes='+myCheckboxes, 
      success: function(data){ 
       $('#myResponse').html(data) 
      } 
     }); 
     return false; 
}); 
}); 
} 
</script> 

而且在myurl.php可以使用print_r($_POST['myCheckboxes']);

1
$.post("test.php", { 'choices[]': ["Jon", "Susan"] }); 

所以我只想遍历检查框和建立数组。就像是。

 var data = { 'user_ids[]' : []}; 
     $(":checked").each(function() { 
     data['user_ids[]'].push($(this).val()); 
     }); 
     $.post("ajax.php", data); 
0

你也可以尝试这个,

var arr = $('input[name="myCheckboxes[]"]').map(function(){ 
    return $(this).val(); 
}).get(); 

console.log(arr);