簡體   English   中英

刪除在php中使用jquery ajax從另一頁獲取的結果

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM