Link to home
Create AccountLog in
Avatar of usmbay
usmbayFlag for United States of America

asked on

pass drop down value to php

Hello,

I want to return the value from drop down list to use it in php script

<form name="test" method="post" action="--WEBBOT-SELF--">
      <select size="1" name="period" onChange="getValue(this.form)">
      <option selected value="all">Select a year</option>
      <option value="2008">2008</option>
      <option value="2007">2007</option>
      </select>
</form>

<SCRIPT language=JavaScript>
function getValue(form) {

var val=form.period.options[form.period.options.selectedIndex].value;

}
</script>


<FORM name="form1" method="post" action="access.php?market=ca">
      <SELECT name="supplier" onChange="document.getElementById('form1').submit();">
            <OPTION value=""></OPTION>
            <?PHP echo $supplierCA;?>
      </SELECT>
</FORM>>>

so the user select the year and then select a supplier from second dropdown list it goes to access.php
I want read the value of year in this access.php

Thanks
ASKER CERTIFIED SOLUTION
Avatar of rohypnol
rohypnol
Flag of Romania image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Avatar of rstewar
rstewar

You could use a hidden variable in your second form to hold the period value from the first form:
<input type="hidden" name="period" value="<? print $_POST['period']; ?>" />

You would then access them in access.php using post variables.

$period = $_POST['period'];
$supplier = $_POST['supplier'];

Avatar of AlexanderR
Do you mind having the data traveling to access.php in GET only?
If not, the its as simple as creating another JS function which collects data from all the forms and then sends it off to access.php.

Use this JS function and then change
<SELECT name="supplier" onChange="document.getElementById('form1').submit();">
to
<SELECT name="supplier" onChange="sendValues();">
function sendValues() {
// get forms
var yearform = document.getElementById('test');
var supplierform = document.getElementById('form1');
 
// get selected values from forms
var year = yearform.period.options[yearform.period.options.selectedIndex].value;
var supplier = supplierform.options[supplierform.supplier.options.selectedIndex].value;
 
// create full URL
var address = 'access.php?market=ca&year='+year+'&supplier='+supplier;
 
// go there
window.locaction=address;
}

Open in new window

Avatar of usmbay

ASKER

Thanks