Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/462.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 更改提交表单时的复选框_Php_Javascript_Jquery_Mysql - Fatal编程技术网

Php 更改提交表单时的复选框

Php 更改提交表单时的复选框,php,javascript,jquery,mysql,Php,Javascript,Jquery,Mysql,我拥有的是mySQL数据库中的一个项目数组,每个项目都有一个复选框。我正在尝试这样做,当你点击复选框时,它会将选中或未选中项目的信息提交到数据库。我有它是取消选中=1和选中=0。这是我要显示项目的位置 现在我的问题是,我似乎无法将任何内容提交到我的数据库中,我对jQuery的理解不够,无法为它编写函数,所以我需要一些帮助。这是我的代码 if(isset($_POST['submit'])){ foreach($_POST['id'] as $id){ $v

我拥有的是mySQL数据库中的一个项目数组,每个项目都有一个复选框。我正在尝试这样做,当你点击复选框时,它会将选中或未选中项目的信息提交到数据库。我有它是取消选中=1和选中=0。这是我要显示项目的位置

现在我的问题是,我似乎无法将任何内容提交到我的数据库中,我对jQuery的理解不够,无法为它编写函数,所以我需要一些帮助。这是我的代码

if(isset($_POST['submit'])){
        foreach($_POST['id'] as $id){
            $value = (isset($_POST['location'][$id]) && $_POST['location'][$id]=="0" ? '0' : '1');
            $insert = mysql_query("UPDATE items SET location='$value' WHERE id='$id'") or die('Insert Error: '.mysql_error());
        }
    }
echo '<form id="form1" method="post"><input type="submit" name="submit" value="Submit">';
$result = mysql_query("SELECT * FROM items")
    or die("Query Failed: ".mysql_error());
    $counter = 0;
    echo '<div class="specialcontainer">';
while($row = mysql_fetch_array($result)){
    list($id, $item_info, $item_img, $price, $sale, $location) = $row;
    if($location == '0'){
        $set_checked = ' checked="checked" ';
    }else{
        $set_checked = '';
    }
    if($counter % 5==0) {
        echo '</div>';
        echo '<div class="specialcontainer">';
    }
    echo '<div class="special"><img src="../images/items/'.$item_img.'" width="130" /><br />';
    echo $item_info.'<br />';
    echo 'Reg. $'.$price.'<br />';
    echo 'Sale $'.$sale.'<br />';
    echo 'Slide Show: <input type="checkbox" id="ch" value="0" name="location['.$id.']"'.$set_checked.' /><br />';
    echo '<input type="button" value="Edit" name="edit" onclick="window.location.href=\'specials.php?action=edit&id='.$id.'\'">';
    echo '<input type="button" value="Delete" name="Delete" onclick="window.location.href=\'specials.php?action=delete&id='.$id.'\'">';
    echo '<input type="hidden" name="id[]" value='.$id.' />';
    echo '</div>';
    $counter++;
}
echo '</div>';
echo '<input type="submit" name="submit" value="Submit"></form>';
if(isset($\u POST['submit'])){
foreach($_POST['id']作为$id){
$value=(isset($\u POST['location'][$id])&&&$\u POST['location'][$id]==“0”?“0”:“1”);
$insert=mysql_查询(“更新项目集位置='$value',其中id='$id')或die('insert Error:'.mysql_Error());
}
}
回声';
$result=mysql\u查询(“从项目中选择*)
或者死(“查询失败:.mysql_error());
$counter=0;
回声';
while($row=mysql\u fetch\u数组($result)){
列表($id、$item\u info、$item\u img、$price、$sale、$location)=$row;
如果($location==“0”){
$set_checked='checked=“checked”;
}否则{
$set_checked='';
}
如果($counter%5==0){
回声';
回声';
}
回声“
”; 回显$item_信息。“
”; 回显'Reg.$'.$price'.
; 回音“销售$”.“销售$”
; echo“幻灯片放映:
”; 回声'; 回声'; 回声'; 回声'; $counter++; } 回声'; 回声';

如你所见,我确实有提交按钮,但我的计划是在onChange提交中删除它。我在checkbox参数中尝试了onchange=“this.form.submit();”,但它不能正常工作。因此,我只想在复选框被点击时提交它。

您应该尝试在复选框的
onchange
方法中使用
document.getElementById('form1')。submit()
,Ajax解决方案是否可以这样工作

$('ch').click(function() {
    //this is what goes into $_POST
    var data = 'id='+ $(this).attr('name') +'&checked=' + $(this).is(':checked');
    $.ajax({
        //This would be the url that would handle updating the MySQL Record
        url: my_mysql_handler.php,
        cache: false,
        type: 'POST',
        data: data,
        success: function(response) {
            alert(response); //or whatever you want to do with the success/fail notification
        }
    });
});

嗯,我试了一下,什么也没发生。我甚至不知道我是否正确使用了这个功能哈哈。就像这个函数会被放在一个??哦,我试过这个和其他很多类似的函数,它只是不发送$u post信息。就像我单击复选框时,它将重新加载页面,但我单击的复选框不会更改。可能在更改复选框值之前调用了onchange方法。如果检查后提交,一切正常,那么这就是问题所在。要解决它:
onchange=“this.checked=!this.checked;document.getElementById('form1').submit()”
是的,这就是我所想的。我尝试了你的第二段脚本,现在我的复选框甚至不再勾选或取消勾选了,哈哈