php 如何在php中创建确认是/否?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36907601/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to create confirm yes/no in php?
提问by Thanh Nguyen
I want to create a confirm yes/no box in php my code like this:
我想在 php 我的代码中创建一个确认是/否框,如下所示:
<?php
if(isset($_REQUEST['id']))
{
?>
<script>
var cf=confirm("do you want to delete Y/N");
if(cf)
{ i want to call code edit of php
}
</script>
<?php
}
?>
<html>
<head>
</head>
<body>
<form name="frm" method="post" action="edit.php">
<a href="edit.php?id=1">Edit </a> <br>
<a href="edit.php?id=2">Edit </a> <br>
<a href="edit.php?id=3">Edit </a> <br>
</form>
</body>
</html>
I Want to when press Yes i call code edit in PHP But it do not work. Can you help me ? Thanks you
我想当按是时我调用 PHP 中的代码编辑但它不起作用。你能帮助我吗 ?谢谢
回答by Murad Hasan
Just use inline onclick event.
只需使用内联 onclick 事件。
This is a simple techique, you can use it in your PHP page.
这是一个简单的技术,您可以在您的 PHP 页面中使用它。
<a href="edit.php?id=1" onclick="return confirm('do you want to delete Y/N')">Edit </a>
回答by Pupil
In your code, you have mentioned PHP but, have used JavaScript.
在您的代码中,您提到了 PHP,但使用了 JavaScript。
If you want to do a confirm with PHP,
如果你想用 PHP 做一个确认,
Create an intermediate page for confirmation.
创建一个中间页面进行确认。
Post form data there.
在那里发布表单数据。
On confirmation page, add two submit buttons:
在确认页面,添加两个提交按钮:
Yes
: If pressed this, redirect/post to edit page.
Yes
:如果按下这个,重定向/发布到编辑页面。
No
: If pressed this, redirect back to form
No
: 如果按下这个,重定向回表单
So, your confirmation page should be:
因此,您的确认页面应该是:
<html>
<head>
</head>
<body>
<?php
if (isset($_POST['confirm'])) {
if ($_POST['confirm'] == 'Yes') {
header("Location:edit.php?id=1");
}
else if ($_POST['confirm'] == 'No') {
header("goBack.php");
}
}
?>
<form method="post">
<?php
if(isset($_REQUEST['id']))
{
?>
<input type="submit" name="confirm" value="Yes"><br/>
<input type="submit" name="confirm" value="No"><br/>
<?php
}
?>
</form>
回答by dimlucas
Put an id on your form:
在您的表单上放置一个 id:
Create an event listener for the form's onsubmit
event
为表单的onsubmit
事件创建一个事件监听器
<script>
function onFormSubmission(e){
return confirm("do you want to delete Y/N");
}
var frm = document.getElementById('frm');
frm.addEventListener("submit", onFormSubmission);
</script>
When the user submits a form they will be prompted with your message. If they click Yes the function will return true and the form will be submitted. Otherwise the function will return false and the form submission will be cancelled
当用户提交表单时,他们将收到您的消息提示。如果他们单击 Yes,函数将返回 true 并提交表单。否则函数将返回 false 并取消表单提交
回答by Poiz
I think this is what you want to do:
我认为这就是你想要做的:
<?php
//YOU MUST BE SURE THAT YOUR URL CONTAINS THE $_REQUEST['id'] PARAMETER, OTHERWISE IT WON'T WORK FROM YOUR CODE... IF YOU WANT IT TO WORK REGARDLESS OF THAT, JUST COMMENT OUT THE IF(ISSET(... BLOCK...
$editURL = "edit.php"; //EDIT URL HERE
if(isset($_REQUEST['id'])) {
//ASSIGN THE ID TO A VARIABLE FOR BUILDING THE URL LATER IN JS...
//THE DEFAULT ID IS 1 BUT YOU CAN DECIDE WITH YOUR OWN LOGIC
$defaultID = ($dID = intval(trim($_REQUEST['id']))) ? $dID : 1;
?>
<script>
function confirmEdit(evt){
evt.preventDefault();
var cf=confirm("do you want to delete Y/N");
var id=<?php echo defaultID; ?>;
if(cf){
//i want to call code edit of php
//HERE'S THE CODE YOU MAY NEED TO RUN;
if(id){
//RETURN TRUE SO THAT THE SCRIPT WITH LINK TO THE APPROPRIATE URL
return true;
// OR REDIRECT WITH JAVASCRIPT TO EDIT PAGE WITH APPROPRIATE ID
//window.location = "" + <?php echo $editURL; ?> + "?id=" + id; //YOU ALREADY HAVE THE EDIT URL... JUST APPEND THE QUERY-STRING WITH ID TO USE IN THE EDIT PAGE
// You might also just (without redirecting) return true here so to that the page continues like you just clicked on the link itself...
}
}
}
</script>
<?php
}
?>
<html>
<head>
</head>
<body>
<!-- THE FORM TAG IS NOT NECESSARY IN THIS CASE SINCE YOUR ANCHOR TAGS HAVE THE EXACT URL YOU WANT ASSOCIATED WITH THEM... AND YOU DON'T EVEN NEED JAVASCRIPT IN THIS CASE... BECAUSE THE HREF OF THE LINKS ARE HARD-CODED... -->
<!-- <form name="frm" method="post" action="edit.php"> -->
<a class='class-4-css' onclick="confirmEdit();" id='dynamic-id-based-btn-1' href="edit.php?id=1">Edit Page 1 </a> <br>
<a class='class-4-css' onclick="confirmEdit();" id='dynamic-id-based-btn-2' href="edit.php?id=2">Edit Page 2</a> <br>
<a class='class-4-css' onclick="confirmEdit();" id='dynamic-id-based-btn-3' href="edit.php?id=3">Edit Page 3</a> <br>
<!-- </form> -->
</body>
</html>
So, now clicking on any of the Links will Ask me to confirm if I want to delete the Resource or not. If I choose yes, then the appropriate page is loaded for the Process...
因此,现在单击任何链接都会要求我确认是否要删除资源。如果我选择是,则会为流程加载相应的页面...
回答by JhonZee
not sure if the other answers really answered your question, this was my problem too, then I experimented and here's what I came up with:
不确定其他答案是否真的回答了您的问题,这也是我的问题,然后我进行了实验,这就是我想出的:
.
.
confirmation in php :
在 php 中确认:
$Confirmation = "<script> window.confirm('Your confirmation message here');
</script>";
echo $Confirmation;
if ($Confirmation == true) {
your code goes here
}
that's all, other people might look for this, you're welcome :)
就是这样,其他人可能会寻找这个,不客气:)
回答by Sandman
I was looking to simply have a confirmation box in php before triggering POST isset without going through javascript:
我希望在不通过 javascript 的情况下触发 POSTisset 之前在 php 中有一个确认框:
echo "<input id='send_btn' type='submit' value='previous'
name='previous' OnClick=\"return confirm('Are you sure you want to go
to previous');\" >";
This appeared for me to be the easiest solution.
这对我来说似乎是最简单的解决方案。