Passing variable via drop-down menu into URL

Logical

Banned
Dec 14, 2010
95
0
0
I guess that describes what I need.

I'd like to create a page that has a drop-down menu with several choices, and based on what choice the user makes, I want it to pass a variable according to which option was selected. Something like:

[Menu]
Choice 1
Choice 2
Choice 3

User selects Choice 1 -> pass "var1"
User selects Choice 2 -> pass "var2"
etc.

The submit button will be linked to a URL that would be something like this: example.com/page.html?option={variable}&othervariables&blahblahblah

where {variable} would be replaced by var1, var2, or whatever the user selected.

So, a variable passed from a drop-down menu into a section of a link. If it helps, my page would be a PHP page and the destination URL would be HTML.

I'll send my entire Paypal balance ($9) to whoever can write this (assuming it's not too hairy) or direct me to a page with such a script on it.
 


I was under the impression that whatever you name your input variable (drop down menu item):

Last name: <input type="text" name="lastname" />

In this case, "lastname" would be the variable that you append to the url when the submit button is clicked. So if you do a submit type of POST like so:

<form name="input" action="html_form_action.php" method="post">

You can retrieve the variable "lastname" on the page html_form_action.php by doing an extract:

extract($_POST);

and you can use teh variable throughout the page, simply by using the variable $lastname.

Does this make sense?
 
To be more specific, say you have a form called "mydropdown"

<form name="input" action="html_form_action.php" method="post">
<select name="mydropdown">
<option value="Milk">Fresh Milk</option>
<option value="Cheese">Old Cheese</option>
<option value="Bread">Hot Bread</option>
</select>
</form>

At the top of your html_form_action.php file, put this line:

extract($_POST);

or use GET (extract($_GET);) if you want to have the variables viewed in the address bar. Keep in mind that this is not a good idea if you can handle POST variables:

Then you can use PHP to extract the variable. Since you don't know whether it is going to be "Milk" "Cheese" or "Bread" you can do something like this.

<?
if($Milk != NULL){
$form_variable = $Milk;
}
elseif($Cheese != NULL){
$form_variable = $Cheese;
}
elseif($Bread!= NULL){
$form_variable = $Bread;
}
?>
and then freely use your newly grabbed variable $form_variable throughout the page wherever you please.

Hope this helps.

-Jeff
 
Thanks for your help!

So, I would create a PHP file with the extract code and then the other code below it? Would my link then be example.com/page.html?option=$form_variable&blahblahblah?
 
To clarify, the destination page is not mine so I can't insert any code onto it. I need this script to add a variable into my affiliate link based on which option the user chooses.
 
Here you go...

<form action="http://www.example.com/page.html" method="get">
<select name="var_name">
<option value="var1">Value 1</option>
<option value="var2">Value 1</option>
<option value="var3">Value 1</option>
<option value="var4">Value 1</option>
</select>
<input type="hidden" name="input_name" value="input_value">
<input type="submit" />
</form>
Use the hidden inputs for whatever you need to pass.
 
Thanks for your help!

So, I would create a PHP file with the extract code and then the other code below it? Would my link then be example.com/page.html?option=$form_variable&blahblahblah?

In order to do that, you will need your form action to do a $PHP_SELF redirect, which will pass the chosen user variable into this page, which has the extract code I mentioned before at the beginning of the page. Then you can have a piece of code that then navigates to your affiliate page with the format you've typed here. Also, what was said below about the 'hidden' is something to look into, I was just piecing together an example above.
 
WINNER! invi789 sent me a small script that works perfectly. Thanks a bunch to Jeff-DBA for helping me get on the right track.