This question already has an answer here:
这个问题在这里已有答案:
- how do i write a javascript alert box to give a yes or no question and integrate with php calls? 1 answer
我如何编写一个javascript警告框来提出是或否问题并与php调用集成? 1个答案
i'm reposting this post i just posted a while ago. how do i write a javascript alert box to give a yes or no question and integrate with php calls?
我刚刚发布了这篇文章,我刚刚发布了这篇文章。我如何编写一个javascript警告框来提出是或否问题并与php调用集成?
i wasnt getting anymore responses so i figured the post got lost in somewhere out there. what i tried doing was this
我没有得到任何回应所以我认为这个帖子在那里的某个地方迷路了。我试过做的是这个
<script type="text/javascript">
if(window.confirm("Are you sure you want to delete that record?")) {
<?php
mysql_query("delete from tbl_payments where id = '$id'") or die(mysql_error());
header("Location: dashboard.php");
?>
}
</script>
and it didnt work. the record just got deleted once i pressed the link to delete. what am i doing wrong? thanks
它没有用。按下链接删除后,记录刚删除。我究竟做错了什么?谢谢
ps: i cant do ajax yet im looking for a simple way of doing it so i can comprehend how it works
ps:我不能做ajax但我正在寻找一种简单的方法,这样我就能理解它是如何工作的
thanks
3 个解决方案
#1
You cannot use JavaScript to control the flow of PHP code in the way you've provided. The reason the record is always deleted is because the PHP interpreter kicks in as soon as it sees the <?php
identifier, so the statement will always be executed.
您不能使用JavaScript以您提供的方式控制PHP代码的流程。记录总是被删除的原因是因为PHP解释器在看到
I would try something like this:
我会尝试这样的事情:
- Use a standard form and the
post
method to submit the id of the record to be deleted - Use JavaScript to enhance the form; bind to the
submit
event and then you can ask for confirmation, only allowing the form to submit if they say okay.
使用标准表单和post方法提交要删除的记录的ID
使用JavaScript来增强表单;绑定到提交事件,然后您可以要求确认,只允许表单提交,如果他们说好。
In this way, the JavaScript enhances functionality and it will degrade gracefully. You'll still have to implement all the business logic of deleting the row from the PHP script that handles the form submission.
通过这种方式,JavaScript增强了功能,并且它会优雅地降级。您仍然必须实现从处理表单提交的PHP脚本中删除行的所有业务逻辑。
#2
From what I can gather you are failing to understand that PHP runs on the server while Javascript runs on the client. Follow @Peter's advice, also read and learn more before diving head first into it and coming up with weirdness like that. A rudimentary fix would be something like:
从我可以收集的内容来看,您无法理解当Javascript在客户端上运行时,PHP在服务器上运行。按照@Peter的建议,先阅读并了解更多信息,然后再先进入潜水,然后想出这样的奇怪之处。一个基本的解决方案是这样的:
<script type="text/javascript">
function deleteRecord(id) {
if(window.confirm("Are you sure you want to delete that record?")) {
window.location.href = 'myScript.php?id=' + id;
}
}
</script>
In your myScript.php:
在你的myScript.php中:
<?php
$id = $_GET['id'];
mysql_query("delete from tbl_payments where id = '$id'") or die(mysql_error());
header("Location: dashboard.php");
?>
#3
you can use something like this:
你可以使用这样的东西:
<script type="text/javascript">
function askUser() {
return window.confirm("Are you sure you want to delete that record?");
}
</script>
and then in your html:
然后在你的HTML中:
<a href="delete.php?id=42" onclick="return askUser();">delete record</a>
of course you need to create a file delete.php which handles all your logic for deleting.
当然你需要创建一个文件delete.php来处理所有删除的逻辑。
ps. in real life you should avoid inline javascript, i'm just using it here for this short example
PS。在现实生活中你应该避免使用内联javascript,我只是在这里用它来做这个简短的例子
#1
You cannot use JavaScript to control the flow of PHP code in the way you've provided. The reason the record is always deleted is because the PHP interpreter kicks in as soon as it sees the <?php
identifier, so the statement will always be executed.
您不能使用JavaScript以您提供的方式控制PHP代码的流程。记录总是被删除的原因是因为PHP解释器在看到
I would try something like this:
我会尝试这样的事情:
- Use a standard form and the
post
method to submit the id of the record to be deleted - Use JavaScript to enhance the form; bind to the
submit
event and then you can ask for confirmation, only allowing the form to submit if they say okay.
使用标准表单和post方法提交要删除的记录的ID
使用JavaScript来增强表单;绑定到提交事件,然后您可以要求确认,只允许表单提交,如果他们说好。
In this way, the JavaScript enhances functionality and it will degrade gracefully. You'll still have to implement all the business logic of deleting the row from the PHP script that handles the form submission.
通过这种方式,JavaScript增强了功能,并且它会优雅地降级。您仍然必须实现从处理表单提交的PHP脚本中删除行的所有业务逻辑。
#2
From what I can gather you are failing to understand that PHP runs on the server while Javascript runs on the client. Follow @Peter's advice, also read and learn more before diving head first into it and coming up with weirdness like that. A rudimentary fix would be something like:
从我可以收集的内容来看,您无法理解当Javascript在客户端上运行时,PHP在服务器上运行。按照@Peter的建议,先阅读并了解更多信息,然后再先进入潜水,然后想出这样的奇怪之处。一个基本的解决方案是这样的:
<script type="text/javascript">
function deleteRecord(id) {
if(window.confirm("Are you sure you want to delete that record?")) {
window.location.href = 'myScript.php?id=' + id;
}
}
</script>
In your myScript.php:
在你的myScript.php中:
<?php
$id = $_GET['id'];
mysql_query("delete from tbl_payments where id = '$id'") or die(mysql_error());
header("Location: dashboard.php");
?>
#3
you can use something like this:
你可以使用这样的东西:
<script type="text/javascript">
function askUser() {
return window.confirm("Are you sure you want to delete that record?");
}
</script>
and then in your html:
然后在你的HTML中:
<a href="delete.php?id=42" onclick="return askUser();">delete record</a>
of course you need to create a file delete.php which handles all your logic for deleting.
当然你需要创建一个文件delete.php来处理所有删除的逻辑。
ps. in real life you should avoid inline javascript, i'm just using it here for this short example
PS。在现实生活中你应该避免使用内联javascript,我只是在这里用它来做这个简短的例子