简体   繁体   English

使用 html 和 javascript 来允许是否调用表单操作不起作用(在 php 中回显)

[英]Using html and javascript to allow whether a form action gets called or not is not working (echoed in php)

I initially had some code written in PHP / HTML that when a button was clicked, the form action took the user to the PHP page called delete.php. I initially had some code written in PHP / HTML that when a button was clicked, the form action took the user to the PHP page called delete.php.

Now i want to add a function in javascript that asks the user whether they are sure they want to go through with the action once they click the button, but I'm not sure how to not let the PHP form submit the form action if the user chooses to cancel. Now i want to add a function in javascript that asks the user whether they are sure they want to go through with the action once they click the button, but I'm not sure how to not let the PHP form submit the form action if the用户选择取消。

This is the code:这是代码:

    #delete buttons    previous code: <form action='delete.php' method='post'>
    echo "<form onsubmit='return setAction(this)' method='post'>";
    echo "<td> <button onclick='askConfirm()' type='submit' name='deleteItem' value='" . $line['productCode'] . "' />Delete</button></td>";
    echo "</form>";

    # end table row and loop
    echo "</tr>";
    
    # javascript
    echo '<script type="text/javascript">

    function setAction(){

      if (confirm("Are you sure you want to delete this?") == true) {
        form.action = "delete.php";

      } 
      else {
        return false;
      }
    }

    </script>';

The solution is actually quite simple.解决方案其实很简单。 This onsubmit attribute will prevent form submission if the confirmation dialogue is cancelled.如果取消确认对话,此onsubmit属性将阻止表单提交。

 <form action="delete.php" onsubmit="return confirm('Do you really want to submit the form?');" method="post"> <input type="submit" name="submit"> </form>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM