2012-04-19 93 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