View Single Post
Join Date: Nov 2007
Posts: 3,761
Reputation: nav33n is a jewel in the rough nav33n is a jewel in the rough nav33n is a jewel in the rough 
Solved Threads: 332
Moderator
Featured Poster
nav33n's Avatar
nav33n nav33n is offline Offline
Senior Poster

Re: Onclick to pass a variable to a PHP script

 
0
  #2
Jun 24th, 2008
and I need to pass the variable "1stOtherStuff" (which is an element in an array) to a PHP script, how do I do that?
Have an onchange event for select tag. Submit the page in onchange event. When the form is submitted, you can access the value of the 1stOtherStuff this way.
$select_value = $_REQUEST['1stOtherStuff']; Here is a simple example.
  1. <?php
  2. $selected_value = $_REQUEST['select'];
  3. echo "Selected value is ". $selected_value;
  4. ?>
  5. <html>
  6. <body>
  7. <form name="form1" method="post" action="onchange.php">
  8. <select name="select" onchange="javascript: document.form1.submit();">
  9. <option value=1>1</option>
  10. <option value=2>2</option>
  11. <option value=3>3</option>
  12. </select>
  13. <input type="button" name="button" value="Click me too!">
  14. </form>
  15. </body>
  16. </html>
If you don't use onchange event, then you can use a submit button. When the button is clicked, the value of select tag is passed to the calling script.
  1. <?php
  2. if(isset($_REQUEST['submit'])) {
  3. $selected_value = $_REQUEST['select'];
  4. echo "Selected value is ". $selected_value;
  5. }
  6. ?>
  7. <html>
  8. <body>
  9. <form name="form1" method="post" action="onchange.php">
  10. <select name="select">
  11. <option value=1>1</option>
  12. <option value=2>2</option>
  13. <option value=3>3</option>
  14. </select>
  15. <input type="submit" name="submit" value="Submit">
  16. </form>
  17. </body>
  18. </html>
If you still have any doubts, let us know
Last edited by nav33n; Jun 24th, 2008 at 2:09 pm.
Ignorance is definitely not bliss!

*PM asking for help will be ignored*
Reply With Quote