I'm very interested in if it is possible to connect javascript variables to php. I know that in php we can write javascript code, but on the contrary we could not. To express my aim better , lets bring example like that:
<form name="some" action="<?php $_SERVER['php_self']; ?>" method="post">
<input type="submit" name="but" value="Action">
</form>
My question is how to make after pressing submit button to confirm (alert) with javascript and if it is confirmed do something (with php) and if isn't cancel (php operation).
-
You should be reading about AJAX...nico– nico2011年12月28日 15:20:34 +00:00Commented Dec 28, 2011 at 15:20
2 Answers 2
You can do two things to pass javascript variable to php:
You can pass it as a hidden input field and submit it using POST
<input id="myHidden" name="myHidden" type="hidden"/>Assign javascript variable to hidden input something like
var myVariable;document.getElementById("myHidden").value = myVariable;You can pass it as a query string with in your URL
As for confirming you can use javascript confirm, which will post on OK and not post/cancel on CANCEL
Something like:
<input type="submit" onclick="return confirm('Are you sure you want to submit?')"/>
<form name="some" action="<?php $_SERVER['php_self']; ?>" method="POST"
onsubmit="document.getElementById("response")
= confirm('some question') ? 'yes' : 'no';
return true;">
<input type="hidden" name="response" id="response" value="">
<input type="submit" name="but" value="Action">
</form>
BTW action="<?php $_SERVER['php_self']; ?>" opens up your page to XSS attacks.