Onclick to pass a variable to a PHP script

Reply

Join Date: Jun 2008
Posts: 11
Reputation: mgt is an unknown quantity at this point 
Solved Threads: 0
mgt mgt is offline Offline
Newbie Poster

Onclick to pass a variable to a PHP script

 
0
  #1
Jun 24th, 2008
If I have the following code:

<form>
.
.
.
<select id="lstOtherStuff" multiple="multiple" size="6" style="width:200px;">
</select>
</form>


and I need to pass the variable "1stOtherStuff" (which is an element in an array) to a PHP script, how do I do that?





If I use the following code, I can access the file "welcome.php" (where I'd run a MYSQL script to query a value from a database):

<form action="welcome.php" method="post">

Would there be another way to access this file, "welcome.php"?


Or can I put the PHP script immediately after the Select script?


I would like to also use an "onclick" command to initiate the action. So the user would press a button, after making a selection (the selection would then be stored in the variable
"1stOtherStuff"). The selected value would then be sent to the PHP script and the PHP script would "echo" the value back.


The main question I have though is how do I pass the variable, "1stOtherStuff", to the PHP/MySQL script...


Your help would be appreciated!
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 3,809
Reputation: nav33n is a jewel in the rough nav33n is a jewel in the rough nav33n is a jewel in the rough nav33n is a jewel in the rough 
Solved Threads: 341
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 Quick reply to this message  
Join Date: Jun 2006
Posts: 7,726
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 487
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Onclick to pass a variable to a PHP script

 
0
  #3
Jun 24th, 2008
@mgt:

Don't create multiple threads for the same topic. It ends up wasting the time of people who reply to those threads thinking there was no reply given.
I don't accept change; I don't deserve to live.

Sacrifice is a painful, pure and beautiful thing.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 8679 | Replies: 2
Thread Tools Search this Thread



Tag cloud for JavaScript / DHTML / AJAX
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2010 DaniWeb® LLC