954,152 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Onclick to pass a variable to a PHP script

If I have the following code:


.
.
.


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

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!

mgt
Light Poster
28 posts since Jun 2008
Reputation Points: 8
Solved Threads: 0
 
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 the1stOtherStuff this way.
$select_value = $_REQUEST['1stOtherStuff'];
Here is a simple example.

<?php
$selected_value = $_REQUEST['select'];
echo "Selected value is ". $selected_value;
?>
<html>
<body>
<form name="form1" method="post" action="onchange.php">
<select name="select" onchange="javascript: document.form1.submit();">
<option value=1>1</option>
<option value=2>2</option>
<option value=3>3</option>
</select>
<input type="button" name="button" value="Click me too!">
</form>
</body>
</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
if(isset($_REQUEST['submit'])) {
$selected_value = $_REQUEST['select'];
echo "Selected value is ". $selected_value;
}
?>
<html>
<body>
<form name="form1" method="post" action="onchange.php">
<select name="select">
<option value=1>1</option>
<option value=2>2</option>
<option value=3>3</option>
</select>
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>

If you still have any doubts, let us know :)

nav33n
Purple hazed!
Moderator
4,465 posts since Nov 2007
Reputation Points: 524
Solved Threads: 356
 

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

~s.o.s~
Failure as a human
Administrator
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 733
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You