user878729
user878729

Reputation: 93

Try to combine javascript confirm box with php post method?

Here is the Javascript code, just for test:

<script type="text/javascript">
    function show_confirm()
        {
            var r=confirm("Are you sure?");
            if (r==true)
            {                   
            }
            else
            {                    
            }
        } 
</script>

And this is the php code:

<form action="Test.php" method="post">
    <input type="text" name="test"/>
    <input type="submit" value="submit" onclick="show_confirm()"/>
</form>

So if the submit button is clicked, a confirm box will pop out. And if "OK" button is clicked, the page will be redirected to Test.php and post method will be executed, like a normal php page submit operation. Or else, if the "cancel" button is clicked, i want it stay the same page and post method won't be executed. Is this function can be implemented and how to modify the code? I know PHP is for server and JS is for client. I'm not sure if I can mix them like this. Or I should use Ajax? Thanks for help!

Upvotes: 1

Views: 14093

Answers (4)

rlemon
rlemon

Reputation: 17666

You can do this directly from the button. return false will cancel the submission ... so....

<input type="submit" onclick="javascript:return confirm('Are you sure?');">

if the user clicks 'ok' the dialog will return true, otherwise false.

to be complete here it is in function form.

function show_confirm()
{
  var r = confirm("Are you sure?");
  if(r == true)
  {
     // do something
     return true;
  } else {
     // do something
     return false;
  }
}

edit: reading this almost 4 years later I'm compelled to change it.

function show_confirm()
{
  var r = confirm("Are you sure?");
  if(r)
  {
     // do something for true
  } else {
     // do something for false
  }
  return r;
}

call it with <input type="submit" onclick="javascript:return show_confirm();">

I believe this will also handle the case where nothing is selected but the dialog is closed.

note: inline event listeners should be avoided. attach an event listener with Javascript.

Upvotes: 2

Cam
Cam

Reputation: 15234

The best way to do this is as follows:

<script>
    function show_confirm(){
        return confirm("Are you sure?");
    }
</script>
<input type="submit" onclick="return show_confirm();">

The reason I use show_confirm instead of the built-in function directly is because if you want to change the functionality of the confirm popup, it is much harder if you are using the builtin function everywhere in your code.

Abstracting out this kind of functionality and writing clear, simple code like this will make you a better programmer :)


warning: as indicated in my comments below, this will only work if the user explicitly clicks the submit button. If the user submits the form by some other method, the confirm box will not show. See http://www.w3schools.com/jsref/event_form_onsubmit.asp if you want to show a confirm regardless of how the form was submitted.

Upvotes: 2

Liam
Liam

Reputation: 4055

Simplifying rlemon's answer even further (no need for the javascript:return, also fixed the inner double quotes):

<input type="submit" onclick="return confirm('Are you sure?')" />

Upvotes: 0

genesis
genesis

Reputation: 50966

<script type="text/javascript">
    function show_confirm()
        {
            var r=confirm("Are you sure?");
            if (r==true)
            {     
                 document.location.href= 'page.php';              
            }
            else
            {                    
                 return false;
            }
        } 
</script>

Upvotes: 0

Related Questions