943,845 Members | Top Members by Rank

Ad:
Jun 24th, 2008
0

Onclick to pass a variable to a PHP script

Expand Post »
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!
Similar Threads
mgt
Reputation Points: 8
Solved Threads: 0
Light Poster
mgt is offline Offline
28 posts
since Jun 2008
Jun 24th, 2008
0

Re: Onclick to pass a variable to a PHP script

Quote ...
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.
php Syntax (Toggle Plain Text)
  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.
php Syntax (Toggle Plain Text)
  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.
Moderator
Featured Poster
Reputation Points: 524
Solved Threads: 356
Purple hazed!
nav33n is offline Offline
3,878 posts
since Nov 2007
Jun 24th, 2008
0

Re: Onclick to pass a variable to a PHP script

@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.
Super Moderator
Featured Poster
Reputation Points: 3233
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,871 posts
since Jun 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in JavaScript / DHTML / AJAX Forum Timeline: Storing a selected item into a global variable
Next Thread in JavaScript / DHTML / AJAX Forum Timeline: Trim spaces





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC