我正在尝试做的是分别在复选框选中/取消选中时保存/删除(mysql)数据库中的数据。我已经动态生成了复选框,我想将特定复选框的数据保存在数据库中。同时,如果取消选中该特定复选框数据将从数据库中删除。 谁能告诉我这是否可以在 php 中使用 jquery 或任何其他脚本语言来实现。 提前致谢。
我正在添加一些我尝试过的代码 这是我的“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>
最佳答案
JQuery 将使用 $.ajax 与服务器对话,最好是 POST。
服务器将尝试使用 PHP 和 MySQLi 删除数据库行。
如果成功或不成功,PHP echo 和 die 是一个有用的响应,可供 jQuery 解释。这可能只是 true
和 false
ajax 等待响应,当响应到达时,复选框将保持原样,或者如果删除失败,则将其切换到原来的状态。您可能想确认操作是否成功。
SO 不适合人们编写您的代码,您需要深入研究这一点。对于经验丰富的开发人员来说,这很容易,但如果你不尝试,你就学不到。如果您遇到编程问题,请随时提出进一步的问题。
示例 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
}
}
});
});
关于php - 选中时将数据保存在数据库中,取消选中时删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15422683/