0

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

1
  • You should be reading about AJAX... Commented Dec 28, 2011 at 15:20

2 Answers 2

1

You can do two things to pass javascript variable to php:

  1. 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;

  2. 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?')"/>
Sign up to request clarification or add additional context in comments.

2 Comments

Please bring some example with hidden input, I would be very pleasured :)
Thanks for helping, I never thought to do that like u offered lol :)
0
<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.

2 Comments

that means that it is better to use in action="fileName.php" yes ? And also in this example what role has the hidden type, please give me some explanation :)
Yes, try it and see what gets posted

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.