2012-04-19 111 views
0

我有什么是从mySQL数据库中的项目数组,每个项目都有一个复选框。我试图做到这一点,当你点击复选框时,它会将信息提交给数据库,以查看已选中或未选中的项目。我有它没有选中= 1和检查= 0.这是我想要显示该项目的位置。复选框onChange提交表格

现在我的问题是我似乎无法得到任何提交到我的数据库,我不明白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>'; 

所以,你可以看到,我确实有提交按钮,但我的计划是要删除onChange提交。我试过onchange =“this.form.submit();”在复选框参数中,但它不能正常工作。所以我只是希望它随时提交一个复选框被点击有点事情。

回答

0

你应该尝试document.getElementById('form1').submit()在您的复选框

+0

oh ive试过这个和其他很多喜欢它,它只是不发送$ _post信息。就像当我点击复选框时,它将重新加载页面,但我点击的复选框不会更改 – GuberX 2012-04-19 21:49:03

+0

也许在更改复选框值之前调用onchange方法。如果在支票提交后,一切正常,那就是问题所在。要解决这个问题:'onchange =“this.checked =!this.checked; document.getElementById('form1')。submit()”' – Tronix117 2012-04-19 22:06:15

+0

是的,这是我认为正在发生。我试过你的第二块脚本那里,现在我的复选框甚至不检查或取消选中了再次哈哈 – GuberX 2012-04-19 22:22:39

1

onchange方法将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 
     } 
    }); 
}); 
+0

嗯,我给它一个尝试,没有任何反应。我什至不知道如果即时通讯使用功能哈哈。就像这个函数放在? – GuberX 2012-04-19 21:45:22

+0

是的,这是javascript。 – Nate 2012-04-20 04:30:38