[英]How to display search results on another page using PHP mySQL and jQuery?
[英]delete results fetched from another page using jquery ajax in php
我正在使用swicth case创建一个jquery ajax php添加,删除,显示应用程序,以便压缩代码。 我可以插入和显示记录,但是无法执行删除操作。 这是我正在使用的代码
的index.php
<html>
<head>
</head>
<body>
<table>
<thead>
<tr>
<th>FIRST NAME</th>
<th>LAST NAME</th>
<th>Action</th>
</tr>
</thead>
<tbody id="showtable">
</tbody>
</table>
<input type="text" name="fname" id="fname"><input type="text" name="lname" id="lname"><button name="submit" id="submit" onclick="crudaction('add','');">ADD NEW</button>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"> </script>
<script>
show();
function show(){
var sub = "display";
$.post("crudaction.php",{action : sub}, function(data){
$("#showtable").html(data);
});
}
function crudaction(action,id){
var querystring;
alert(action);
switch(action){
case "add":
querystring ='action='+action+'&fname='+$("#fname").val()+'&lname='+$("#lname").val();
break;
case "delete":
querystring = 'action='+action+'&id='+id;
break;
}
$.ajax({
url : "crudaction.php",
type : "POST",
datatype : "JSON",
cache : false,
data : querystring,
complete : function(){
alert('success');
show();
}
});
}
</script>
</body>
</html>
crudaction.php
<?php
include_once("dbc.php");
$ob1 = new controller();
$action = $_POST['action'];
if(isset($action))
{
switch($action)
{
case "add":
mysqli_query($ob1->con,"insert into ajaxtable(fname,lname) values('".$_POST['fname']."','".$_POST['lname']."') ");
break;
case "delete":
mysqli_query($ob1->con,"delete from ajaxtable where id='".$_POST['id']."'");
break;
case "display":
$res = $ob1->display();
foreach($res as $k=>$val){
echo "<tr>
<td>".$val['fname']."</td>
<td>".$val['lname']."</td>
<td><a href='#' onclick='crudaction('delete','".$val['id']."'>Delete</a></td>
</tr>";
}
break;
}
exit();
}
?>
dbc.php
<?php
class controller{
private $server = "localhost";
private $user = "root";
private $pass = "";
private $dbname = "search";
var $con;
function __construct(){
$this->con = $this->connect();
}
public function connect(){
$this->con = mysqli_connect("$this->server","$this->user","$this->pass","$this->dbname") or die("coudn't connect".mysqli_error($con));
return $this->con;
}
public function display(){
$res = mysqli_query($this->con,"select * from ajaxtable");
if($res->num_rows>0){
while($row = $res->fetch_assoc()){
$result[] =$row;
}
if(!empty($result))
return $result;
}
}
}
?>
无法找出问题所在?
我很确定您的crudaction.php代码中存在引号转义问题和缺少的右括号:
<td><a href='#' onclick='crudaction('delete','".$val['id']."'>Delete</a></td>
它应该是
<td><a href='#' onclick='crudaction(\'delete\',\'".$val['id']."\');'>Delete</a></td>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.