2013-03-15 87 views
0

我想做的是保存/刪除數據在(mysql)數據庫複選框檢查/取消選中分別。我有動態生成的複選框,我想保存數據庫中特定複選框的數據。同時如果取消選中特定複選框數據將從數據庫中刪除。 任何人都可以告訴我這是可能的使用jQuery或任何其他腳本語言在PHP中。 在此先感謝。檢查保存數據庫中的數據,並取消取消

我加入一些代碼是什麼我想這 我「table_edit_ajax.php」代碼

<?php 
$mysql_hostname = "localhost"; 
$mysql_user = "root"; 
$mysql_password = ""; 
$mysql_database = "test"; 
$bd = mysql_connect($mysql_hostname, $mysql_user, $mysql_password) 
or die("Opps some thing went wrong"); 
mysql_select_db($mysql_database, $bd) or die("Opps some thing went wrong"); 


if($_POST[first_input_.$i]) 
{ 
$firstname=mysql_escape_String($_POST[first_input_.$i]); 
$sql = "INSERT INTO fullnames(firstname)VALUES('$firstname')"; 
mysql_query($sql); 
} 
?> 

這是我的主要文件,在其中我生成複選框名爲「test.php的」

<?php 
$mysql_hostname = "localhost"; 
$mysql_user = "root"; 
$mysql_password = ""; 
$mysql_database = "test"; 
$bd = mysql_connect($mysql_hostname, $mysql_user, $mysql_password) 
or die("Opps some thing went wrong"); 
mysql_select_db($mysql_database, $bd) or die("Opps some thing went wrong"); 
?> 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>Untitled Document</title> 
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script> 
<script type="text/javascript"> 
$('.edit_tr').on('change', function(){ 
    // saving or deleting? 
    var savedelete; 
    if ($(this).is(':checked')) savedelete='save'; 
    alert ("save test"); 
    else savedelete='delete'; 
    alert ("delete test"); 
    // what am I changing? 
    var name=$(this).attr('first_input_<?php echo $i; ?>'); 

    // call the server 
    $.ajax({ 
     type: "POST", 
     url: 'table_edit_ajax.php' 
     data: {"do": "saveordelete", "whichone": savedelete, "name": name }, 
     success: function(data){ 
     if (data=='true'){ // is string not boolean 
      // it worked 
      alert ("true"); 
     }else{ 
      // didn't work 
      alert ("false"); 
     } 
     } 
    }); 

}); 
</script> 
<style> 
body 
{ 
font-family:Arial, Helvetica, sans-serif; 
font-size:14px; 
} 

td 
{ 
padding:7px; 
} 
.editbox 
{ 
font-size:14px; 
width:270px; 
/*background-color:#ffffcc;*/ 

border:solid 1px #000; 
padding:4px; 
} 

th 
{ 
font-weight:bold; 
text-align:left; 
padding:4px; 
} 
.head 
{ 
background-color:#333; 
color:#FFFFFF 

} 

</style> 
</head> 

<body bgcolor="#dedede"> 
<div style="margin:0 auto; width:750px; padding:10px; background-color:#fff; height:800px;"> 


<table width="100%"> 
<tr class="head"> 
<th>First Name</th> 
</tr> 
<?php 
$i=1; 
while($i<=5) 
{ 
if($i%2) 
{ 
?> 
<tr id="lovetr<?php echo $i; ?>" class="edit_tr"> 
<?php } else { ?> 
<tr id="trlove<?php echo $i; ?>" bgcolor="#f2f2f2" class="edit_tr"> 
<?php } ?> 
<td width="50%" class="edit_td"> 
<input type="checkbox" value="firstname<?php echo $i; ?>" class="editbox" name="first_input_<?php echo $i; ?>" /> 
</td> 
</tr> 

<?php 
$i++; 
} 
?> 

</table> 
</div> 
</body> 
</html> 
+0

呀這是可能的,但你必須要問一個更具體的問題 – 2013-03-15 00:32:58

+0

@ExplosionPills你能告訴我這件事有關的任何例子... – ShriD 2013-03-15 01:03:16

+0

@ExplosionPills請於固定我的問題有所幫助,我編輯問題並插入我的代碼也可以請幫助理清這個概率。 – ShriD 2013-03-16 01:02:06

回答

2

JQuery將使用$ .ajax與服務器通信,最好是POST。

服務器將嘗試使用PHP和MySQLi刪除數據庫行。
如果成功或不成功的PHP回聲併爲jQuery解析一個有用的響應。這可以簡單地是truefalse

ajax等待這個響應,當它到達時會離開復選框,或者將它切換到刪除失敗時的狀態。您可能想確認該操作是否成功。

因爲人們不會在這裏編寫代碼,所以需要深入研究。對於一個經驗豐富的開發人員來說很簡單,但如果你不嘗試,你就不會學習。如果遇到編程問題,隨時可以提出進一步的問題。

例jQuery的未選中

<label><input type="checkbox" value="1" name="apples" class="savedelete" />Save/Delete</label> 
<label><input type="checkbox" value="1" name="pears" class="savedelete" />Save/Delete</label> 

jQuery的

$('.savedelete').on('change', function(){ 
    // saving or deleting? 
    var savedelete; 
    if ($(this).is(':checked')) savedelete='save'; 
    else savedelete='delete'; 
    // what am I changing? 
    var name=$(this).attr('name'); 

    // call the server 
    $.ajax({ 
     type: "POST", 
     url: 'index.php' 
     data: {"do": "saveordelete", 
     "whichone": savedelete, 
     "name": name 
     }, 
     success: function(data){ 
     if (data=='true'){ // is string not boolean 
      // it worked 
     }else{ 
      // didn't work 
     } 
     } 
    }); 

}); 
+0

可以請告訴我任何示例,因爲我是ajax的新手。 – ShriD 2013-03-15 00:59:35

+0

已經添加了一些示例代碼。你必須閱讀這個http://api.jquery.com/jQuery.post/ – Popnoodles 2013-03-15 01:07:56

+0

我試了很多我不在哪裏我錯了,你可以請告訴我在我的代碼更正。如果你不忙, – ShriD 2013-03-16 00:42:02

0
<?php 
include('../dbcon.php'); 
$id = $_POST['id']; 
mysql_query("delete from files where file_id = '$id' ")or die(mysql_error()); 
?> 

你CAK使用checbox讓id選擇,並加入到$ _ POST [ '選擇'];

<?php 
     include('../dbcon.php'); 
     if (isset($_POST['backup_delete'])){ 
     $id=$_POST['selector']; 
     $N = count($id); 
     for($i=0; $i < $N; $i++) 
     { 
      $result = mysql_query("DELETE FROM teacher_backpack where file_id='$id[$i]'"); 
     } 
     header("location: backack.php"); 
     } 
     ?> 
+0

請爲您的示例提供一些解釋 – mhatch 2017-12-07 21:41:19