复选框动态ID发送到PHP文件而不刷新页面

时间:2013-09-14 16:01:55

标签: javascript php html checkbox

我非常接近这一点,但努力让我的动态PHP和Javascript一起工作。我也在哪里。

<script language="javascript" type="text/javascript">

    function exefunction(id){

    if (document.getElementById(id).checked == true) {
        // Create our XMLHttpRequest object
        var hr = new XMLHttpRequest();
        // Create some variables we need to send to our PHP file
        var url = "update.php";
        var id = document.getElementById(id).value 
        var check = 1;
        var vars = "id="+id+"&check="+check;
        hr.open("POST", url, true);
        // Set content type header information for sending url encoded variables in the request
        hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        // Access the onreadystatechange event for the XMLHttpRequest object
        hr.send(vars); // Actually execute the request
    }

                    else if (document.getElementById(id).checked == false){
                        // Create our XMLHttpRequest object
        var hr = new XMLHttpRequest();
        // Create some variables we need to send to our PHP file
        var url = "update.php";
        var id = document.getElementById(id).value 
        var check = 0;
        var vars = "id="+id+"&check="+check;
        hr.open("POST", url, true);
        // Set content type header information for sending url encoded variables in the request
        hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        // Access the onreadystatechange event for the XMLHttpRequest object
        hr.send(vars); // Actually execute the request

                }
            </script>

我的php会检查数据库并自动检查相应的复选框:

if ($checkboxone == 1){
$Trans .='<td width="60">
<input id="'.$id.'" type="checkbox" onClick="exefunction(this.id)" value="'.$id.'" checked /></td>'; 
}else{
$Trans .='<td width="60"><input id="'.$id.'" type="checkbox" onClick="exefunction(this.id);" value="'.$id.'" /></td>'; 
}

我需要它做的是当选中或取消选中复选框时,它会更新我的数据库我遇到的问题是通过我的javascript传递复选框的唯一ID。如果我将id放入并有一个复选框,则javascript可以正常工作。

希望这是有道理的。

0 个答案:

没有答案
相关问题