-1

When the user clicks the submit button, I was to pop-up a confirm box. My problem is that when he presses OK, I want to run one query. When he presses Cancel, I want to execute a different query.

Can this be achieved with only PHP and JavaScript (Not AJAX or JQuery)

If Yes, Please tell me how. Thanks.

Edit: My main problem is that if I attach the onclick function to submit button, it is executing the server side code before taking the confirmation. And I cannot add the onsubmit function to the form since I have two action items associated with this and I want this feature for only one button. Any thoughts on this?

  • 4
    Add complete and relative code whenever you ask querstion – Tushar Aug 26 '15 at 06:37
  • I have not been able to figure out the method, hence I have no code for the query. – Neha Agarwal Aug 26 '15 at 06:37
  • 1
    SO is not a code genie. You need to research your problem and share what you have tried and why it hasn't worked for you – cameronjonesweb Aug 26 '15 at 06:37
  • You cannot run php code using javascript, you will have to use jquery ajax. You can also redirect to some other .php page depending upon what the user selects in the confirmation box. – Criesto Aug 26 '15 at 06:39

4 Answers4

1

Create a hidden input field. Based on user's selection set it to different values using JS and then parse those values using php script to identify what should be done

DannyPhantom
  • 1,042
  • 10
  • 21
  • Thank You for the suggestion. However my problem is that if I attach the onclick function to submit button, it is executing the server side code before taking the confirmation. And I cannot add the onsubmit function to the form since I have two action items associated with this and I want this feature for only one button. Any thoughts on this? – Neha Agarwal Aug 26 '15 at 06:57
  • @NehaAgarwal event.preventDefault might help you with this. It will make your form think that you never pressed the button. You will have to forge it later though. http://www.w3schools.com/jsref/event_preventdefault.asp – DannyPhantom Aug 26 '15 at 07:01
  • That helped. Thanks a lot :) – Neha Agarwal Aug 26 '15 at 07:46
0

Use Hidden field for user selected value like OK or Cancel.Change the query based on the user selected value.

0

use this way...

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

//if user click ok then your form will submited otherwise if user click cancel it does nothing .  

may be this will help

Sahil Manchal
  • 472
  • 6
  • 20
  • Please consider editing your post to add more explanation about what your code does and why it will solve the problem. An answer that mostly just contains code (even if it's working) usually wont help the OP to understand their problem. It's also recommended that you don't post an answer if it's just a guess. A good answer will have a plausible reason for why it could solve the OP's issue. – SuperBiasedMan Aug 26 '15 at 10:22
0

You can call confirm()

function myfunc1(id) {
    if (confirm("OK to submit?")) {
            location.href = 'delete.php?id='+id;
    }else{
            // somthing else
    }
}

Or you can call it in the onclick:

onclick="if (confirm('OK to submit?')) { myfunc1('<?php echo $uid;?>'); } "
Shailendra Sharma
  • 6,976
  • 2
  • 28
  • 48