RSS Forums RSS

Onclick to pass a variable to a PHP script

Please support our JavaScript / DHTML / AJAX advertiser: Programming Forums
Reply
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

  #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!
AddThis Social Bookmark Button
Reply With Quote  
Posts: 3,618
Reputation: nav33n is a jewel in the rough nav33n is a jewel in the rough nav33n is a jewel in the rough 
Solved Threads: 309
nav33n's Avatar
nav33n nav33n is offline Offline
Senior Poster

Re: Onclick to pass a variable to a PHP script

  #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 1:09 pm.
Ignorance is definitely not bliss!

*PM asking for help will be ignored*
Reply With Quote  
Posts: 7,398
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 
Solved Threads: 439
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

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

Act from reason, and failure makes you rethink and study harder.
Act from faith, and failure makes you blame someone and push harder.

-- Eric Naggum RIP :-(
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.



Views: 5927 | Replies: 2 | Currently Viewing: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 1:23 pm.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC