Reputation: 265
After validating user data from database I want to update some values in the Database using php code.
I have tried
var answer = confirm("Do you want to Checkout?");
if(answer==true)
{
<?php MY UPDATE COMMAND ?>
}
but update command execute even if answer==false
Please help me in solving my problem.
Thanks
Upvotes: 0
Views: 515
Reputation: 8200
javascript is a client side program (in this case) which is ran within a clients browser. Php is a Server side scripting language. Basically php generates an html file and sends it to the client. All php commands are executed before anything happens in javascript because php simply generates an html file. The javascript is loaded in the clients browser.
So the php command will ALWAYS run because it is being called. PHP has no idea that it is surrounded by javascript code and does nothing to read it. It just includes the javascript code in the html file that it sends along.
Upvotes: 0
Reputation: 998
Here are solutions for you:
1st - use AJAX. As for example with help of jQuery:
if (confirm('Do you want to Checkout?')) {
$.ajax({
url: 'logout.php'
}).done(function(){
// do something after php checkout, for example: go to home page
window.location.href = '/';
});
}
2nd - is to use links like this:
<a href="checkout.php"
onclick="return confirm('Do you want to Checkout?')">
Checkout</a>
Upvotes: 1
Reputation: 64526
That can't work because PHP executes on the server side, before any javascript is even sent to the browser. So what will happen is your MY UPDATE COMMAND
will execute first, before the Javascript - which is executed client side in the browser.
If you want to execute some PHP if the confirm is true then you will need to submit a form, or do an AJAX call at that point. I suggest reading up on how PHP and Javascript execute.
Upvotes: 0