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!
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.