如何在php中创建确认是/否?

时间:2016-04-28 07:06:59

标签: php message

我想在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>

我想按下是的,我在PHP中调用代码编辑 但它不起作用。 你能帮助我吗 ? 谢谢你

6 个答案:

答案 0 :(得分:6)

只需使用内联onclick事件。

这是一个简单的技术,您可以在PHP页面中使用它。

<a href="edit.php?id=1" onclick="return  confirm('do you want to delete Y/N')">Edit </a>

答案 1 :(得分:4)

在你的代码中,你提到过PHP,但是使用过JavaScript。

如果您想使用PHP进行确认,

创建一个中间页面进行确认。

在那里发布表单数据。

在确认页面上,添加两个提交按钮:

Yes:如果按下这个,请重定向/发布到编辑页面。

No:如果按此按钮,则重定向回到表单

因此,您的确认页面应为:

<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>

答案 2 :(得分:0)

在表单上添加ID:

为表单的onsubmit事件

创建一个事件监听器
<script>
   function onFormSubmission(e){
       return confirm("do you want to delete Y/N");
   }

   var frm = document.getElementById('frm');
   frm.addEventListener("submit", onFormSubmission);
</script>

当用户提交表单时,系统会提示您输入消息。如果他们单击是,则该函数将返回true并且将提交表单。否则该函数将返回false并且表单提交将被取消

答案 3 :(得分:0)

我认为这就是你想要做的事情:

<?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>

所以,现在点击任何链接将要求我确认是否要删除资源。如果我选择是,则为Process ...

加载适当的页面

答案 4 :(得分:0)

不确定其他答案是否真的回答了你的问题,这也是我的问题,然后我进行了实验,这就是我想出的:

在php中确认:

    $Confirmation = "<script> window.confirm('Your confirmation message here');
        </script>";

    echo $Confirmation;

        if ($Confirmation == true) {
    your code goes here
    }

就是这样,其他人可能会寻找这个,欢迎你。)

答案 5 :(得分:0)

我只是想在触发POST isset之前在php中有一个确认框,而无需通过javascript:

echo "<input id='send_btn' type='submit' value='previous'
name='previous' OnClick=\"return confirm('Are you sure you want to go
to previous');\" >";

这对我来说似乎是最简单的解决方案。