admin管理员组

文章数量:1291050

I've build a decent CMS for my website. It allows me to manage the entire content, as well as delete an article. I just want to make some kind of Javascript(any other suggestions are most wele) stop and ask function. When someone clicks delete(my employees are going to use the CMS in the very near future) show a prompt asking Are you sure? and if they clicks yes, only then go through with the delete. The delete is being done through an url request. .php?action=delete_article&id=whatever_id Something that my quantum physics teacher used to refer to as the anti-idiot protection. Thanks in advance for all your help!

I've build a decent CMS for my website. It allows me to manage the entire content, as well as delete an article. I just want to make some kind of Javascript(any other suggestions are most wele) stop and ask function. When someone clicks delete(my employees are going to use the CMS in the very near future) show a prompt asking Are you sure? and if they clicks yes, only then go through with the delete. The delete is being done through an url request. http://mywebsite./somefile.php?action=delete_article&id=whatever_id Something that my quantum physics teacher used to refer to as the anti-idiot protection. Thanks in advance for all your help!

Share Improve this question asked Feb 14, 2012 at 6:55 user1189402user1189402 3
  • 1 Note that you shouldn't use GET requests to perform an operation that changes data. GET should only be used for retrieving data and have no other side-effect. – Joey Commented Feb 14, 2012 at 7:02
  • Elaborating on what @Joey said, here is a link to learn more: mikeshannon./what-is-difference-between-get-and-post – Alec Smart Commented Feb 14, 2012 at 9:59
  • ye, it was 6 in the morning when I wrote the stuff. just realized i used get when reading your stuff:). Thank you! – user1189402 Commented Feb 14, 2012 at 13:48
Add a ment  | 

4 Answers 4

Reset to default 4
function redirect($redirect,$redirect_2,$message) ///confirm box pop up
    {
        echo "<script>javascript:
        var ask = confirm('".$message."');
        if(ask==true)
        {
            window.location = '".$redirect."';  
        }
        else
        {
            window.location = '".$redirect_2."';    
        }
        </script>";
    }

try something like this function.

Something as simple as will help:

function confirmclick(id) {
    var answer = confirm ('This action cannot be undone. Are you sure you want to perform this action?');
    if (answer) {
              location.href = 'http://mywebsite./somefile.php?action=delete_article&id='+id;
              // Or do an AJAX operation
    }
}

And the link can be <a href="javascript:void(0);" onclick="javascript:confirmclick(XX)">Delete</a>

The most simple solution would be the confirm box.

var deleteIt = confirm("Are you sure you want to delete?");
if (deleteIt) {
    // your delete code here, probably form submission or AJAX
}

I tend to like to have one form that I can retrieve data from with a GET and then make changes with a POST and have all the code in one PHP file. For something like you are doing, here's what I do, where the Validate button makes a GET URL call for querying and validating the results (the URL ensures easy debugging and parameter changes if I want and then if the results look good, the Submit button allows whatever it is I want to be sent via POST. I've thrown in the bonus of including username and password prompts on the submission, but you can strip those out. The PHP input parameters use _REQUEST instead of _GET or _POST since the input could be from either. Here's the HTML. I put the Javascript in the because for some reason it doesn't work for me if I put it in the header.

<html>

<body>
  <form>
    <label for="input1">Input 1:</label>
    <input type="text" id="input1" name="input1"><br><br>

    <label for="input2">Input 2:</label>
    <input type="text" id="input2" name="input2"><br><br>

    <button type="button" onclick="validate()">Validate</button>
    <button type="button" onclick="submitForm()">Submit</button>
  </form>
  <script>
    function validate() {
      let input1 = document.getElementById("input1").value;
      let input2 = document.getElementById("input2").value;
      let url = "formtest.html?input1=" + input1 + "&input2=" + input2;
      window.location.href = url;
    }
    //Test
    function submitForm() {
      let input1 = document.getElementById("input1").value;
      let input2 = document.getElementById("input2").value;
      let confirmed = confirm("Are you sure you want to submit this form?");
      if (confirmed) {
        let username = prompt("Please enter your username:");
        let password = prompt("Please enter your password:");
        let form = document.createElement("form");
        form.setAttribute("method", "POST");
        form.setAttribute("action", "formtest.html");
        let input1Field = document.createElement("input");
        input1Field.setAttribute("type", "hidden");
        input1Field.setAttribute("name", "input1");
        input1Field.setAttribute("value", input1);
        form.appendChild(input1Field);
        let input2Field = document.createElement("input");
        input2Field.setAttribute("type", "hidden");
        input2Field.setAttribute("name", "input2");
        input2Field.setAttribute("value", input2);
        form.appendChild(input2Field);
        if (username && password) {
          let input3Field = document.createElement("input");
          input3Field.setAttribute("type", "hidden");
          input3Field.setAttribute("name", "usr");
          input3Field.setAttribute("value", username);
          form.appendChild(input3Field);
          let input4Field = document.createElement("input");
          input4Field.setAttribute("type", "hidden");
          input4Field.setAttribute("name", "pwd");
          input4Field.setAttribute("value", password);
          form.appendChild(input4Field);
        }
        document.body.appendChild(form);
        form.submit();
      }
    }
  </script>

</body>

</html>

I hope this is useful for someone.

本文标签: javascriptConfirmation prompt before PHP executionStack Overflow