从javascript到php格式的复选框
我有一个基于MySQL数据的带有动态复选框的表单。提交时,DIV将刷新而不闪烁JavaScript。我试图将表单数据发送到PHP以更新MySQL,但由于缺乏JavaScript知识,我经常遇到这样或那样的错误。我当前的尝试给出了TypeError:document.multipix_form.pix是FireBug中未定义的错误从javascript到php格式的复选框,javascript,php,jquery,mysql,Javascript,Php,Jquery,Mysql,我有一个基于MySQL数据的带有动态复选框的表单。提交时,DIV将刷新而不闪烁JavaScript。我试图将表单数据发送到PHP以更新MySQL,但由于缺乏JavaScript知识,我经常遇到这样或那样的错误。我当前的尝试给出了TypeError:document.multipix_form.pix是FireBug中未定义的错误 function multipicupdate(php_file, purpose, where) { var request = getXMLHTTP();
function multipicupdate(php_file, purpose, where) {
var request = getXMLHTTP(); // call the function for the XMLHttpRequest instance
var a = document.getElementById("optone").value ;
var b = document.getElementById("table").value ;
var boxes = document.multipix_form.pix.length
txt = ""
for (i = 0; i < boxes; i++) {
if (document.multipix_form.pix[i].checked) {
txt = txt + document.multipix_form.pix[i].value + " "
}
}
var the_data = 'purpose='+purpose+'&var1='+a+'&var2='+b+'&var3='+txt;
request.open("POST", php_file, true); // set the request
request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
request.send(the_data); // calls the send() method with datas as parameter
request.onreadystatechange = function() {
if (request.readyState == 4) {
document.getElementById(where).innerHTML = request.responseText;
}
}
}
表单名称为multipix_form。三种表单输入分别为optone select、table select和pix[]复选框。正如我前面所说的,pix[]是动态的。让我感到困惑的是将复选框数据从JavaScript传递到php。
我提交的表格是:
<input type="button" onClick="multipicupdate('php/ajaxprocess.php', 'multipix', 'message_profile'); return false;" value="Save changes to this photo">
php将采用表单数据并更新MySQL 后面需要分号
var boxes = document.multipix_form.pix.length
及
正如您标记了jQuery一样,这里有一个jQuery解决方案。您可以大大简化此代码。首先,可以使用serialize从该表单中输入的值创建querystring。然后可以使用$.ajax将该信息发送到所需的页面。试试这个:
<input type="button" value="Save changes to this photo" id="multipicupdate">
好的,将myForm更改为multipix_form my form的id,并将url设置为php脚本,该脚本当前仅为,但我没有获取复选框数据。我遗漏了什么吗?假设这是正确的答案,并且我的复选框数据未序列化的问题是其他问题。谢谢
<input type="button" value="Save changes to this photo" id="multipicupdate">