我相信你们之前有很多人已经看过这个问题,我已经将适用于他人的代码应用到我自己的代码中,但仍然没有,我发现很难确定哪些是错误的。这是我的用户端,其中显示了mysql信息和删除按钮的表:
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "gpdb";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("connection failed: " . $conn->connect_error);
}
$id = $_SESSION['id'];
$sql = "SELECT AppID, AppDate FROM appointment WHERE PatientID ='$id'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo ("<form action='delete.php' method='post'><table class='jobs-list'>
<tr>
<th class='job-position'><h4>Appointment ID</h4></th>
<th class='job-position'><h4>Appointment Date and Time</h4></th>
<th class='job-position'><h4>Edit Appointments</h4></th>
</tr>");
while($row = mysqli_fetch_array($result))
{
echo ('<tr>');
echo ('<td>' . $row['AppID'] . '</td>');
echo ('<td>' . $row['AppDate'] . '</td>');
echo ("<td><input type='submit' id='deleteItem' value='".$row['AppID']."'/> </td>");
echo ('</tr>');
}
echo ('</table></form>');
}
else{
echo('<h4><b>You currently have no appointments</b></h4>');
}
?>
<script>
$('#deleteItem').on('click', function(){
var pass = $(this).val();
console.log(pass);
$.post( "delete.php", {
'pass':pass
}).done(function( data ) {
alert( "You cancelled : " + data );
});
});
</script>
这是delete.php文件:
<?php
session_start();
//$servername = "localhost";
//$username = "root";
//$password = "";
//$dbname = "gpdb";
//$conn = new mysqli($servername, $username, $password, $dbname);
//if ($conn->connect_error) {
//die("connection failed: " . $conn->connect_error);
//}
print_r($_POST['pass']);
//if(isset($_POST['pass']) && $_POST['pass'] != "")
//{
//$deleteItem = $_POST['pass'];
//$sql = "DELETE FROM appointment WHERE AppID='{$deleteItem}'";
//$result = $conn->query($sql);
//header("location: http://localhost/gp/account.php");
//}else{
//echo('Something wrong');
//}
?>
我目前正在读取delete.php上的“错误”,因此delete.php没有返回。
提前感谢您的任何帮助:)
答案 0 :(得分:1)
问题在于您的代码。
你应该像这样回复按钮:echo ("<td><input type='button' class='deleteItem' name='delete_".$row['AppID']."' value='".$row['AppID']."'/>")
你不应该在你的javascript中使用php变量,你的javascript应该是这样的
<script>
$('.deleteItem').on('click', function(){
var pass = $(this).val();
$.post( "delete.php", {
'pass':pass
}).done(function( data ) {
alert( "You cancelled : " + data );
});
});
</script>
然后在您的delete.php中,您应该检查$_POST['pass']
NOT $_POST['deleteItem']
所以代码将是
<?php
session_start();
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "gpdb";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("connection failed: " . $conn->connect_error);
}
if(isset($_POST['pass']) && is_numeric($_POST['pass']))
{
$deleteItem = $_POST['pass'];
$sql = "DELETE FROM appointment WHERE AppID='{$deleteItem}'";
$result = $conn->query($sql);
header("location: http://localhost/gp/account.php");
}else{
echo('Something wrong');
}
?>
希望这会有所帮助。
答案 1 :(得分:0)
这种情况是你的敌人
if(isset($_POST['deleteItem']) and is_numeric($_POST['deleteItem']))
因为$_POST['deleteItem']
不是数字,因为值类似于Delete 1
,这就是为什么你总是看到&#34;错误的东西&#34;
答案 2 :(得分:0)
您使用按钮类型提交它将重新加载页面。使用event.preventDefault();
检查下面的代码,或者将submit
类型更改为button
<script>
$('#deleteItem').on('click', function(event){
event.preventDefault();
var pass = $row['AppID'];
$.post( "delete.php", { 'pass':pass }).done(function( data ) {
alert( "You cancelled : " + data );
});
});
</script>
答案 3 :(得分:0)
终于解决了这个问题,谢谢你的帮助,特别是@Gopakumar Gopalan。我真的需要那个发布调用,但似乎表单方法正在保留函数,所以我从表中删除了表单标签:
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "gpdb";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("connection failed: " . $conn->connect_error);
}
$id = $_SESSION['id'];
$sql = "SELECT AppID, AppDate FROM appointment WHERE PatientID ='$id'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo ("<table class='jobs-list'>
<tr>
<th class='job-position'><h4>Appointment ID</h4></th>
<th class='job-position'><h4>Appointment Date and Time</h4></th>
<th class='job-position'><h4>Edit Appointments</h4></th>
</tr>");
while($row = mysqli_fetch_array($result))
{
echo ('<tr>');
echo ('<td>' . $row['AppID'] . '</td>');
echo ('<td>' . $row['AppDate'] . '</td>');
echo ("<td><input type='submit' class='deleteItem' name='delete_".$row['AppID']."' value='".$row['AppID']."'/></td>");
echo ('</tr>');
}
echo ('</table>');
}
else{
echo('<h4><b>You currently have no appointments</b></h4>');
}
?>
<script>
$('.deleteItem').on('click', function(){
var pass = $(this).val();
$.post( "delete.php", {
'pass':pass
}).done(function( data ) {
location.reload();
});
});
</script>
删除表单标记后,delete.php文件可识别$ _POST [&#39; pass&#39;]; :
<?php
session_start();
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "gpdb";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("connection failed: " . $conn->connect_error);
}
if(isset($_POST['pass']) && $_POST['pass'] != "")
{
$deleteItem = $_POST['pass'];
$sql = "DELETE FROM appointment WHERE AppID='{$deleteItem}'";
$result = $conn->query($sql);
header("location: http://localhost/gp/account.php");
}else{
echo('Something wrong');
}
?>
希望这个答案也可以帮助别人。干杯!
答案 4 :(得分:-1)
#deleteItem首先意味着您正在访问和触发id中的事件,并且页面中没有具有此类名称的ID
第二,如果你将id =“deleteItem”放在循环中会有多个同名的id错误
你可以做的是在一个函数中发送id,它将发送单击删除按钮的行的确切id
<input type='button' name='deleteItem' value='Delete" onclick="deleteSelected('" .$row['AppID']."')'/>
现在在函数内部调用ajax,如下所示
<script>
function deleteSelected(delete_id)
{
$.post( "delete.php", {
'deleteItem':delete_id
}).done(function( data ) {
alert( "You cancelled : " + data );
});
}
</script>
现在将操作页面上的if条件更改为
if(isset($_POST['deleteItem']) && is_numeric($_POST['deleteItem']))
{
$deleteItem = $_POST['deleteItem'];
$sql = "DELETE FROM appointment WHERE AppID='{$deleteItem}'";
$result = $conn->query($sql);
header("location: http://localhost/gp/account.php");
}else{
echo('Something wrong');
}