How can you process the value from only one of two lists?
Use the onchange event attribute to load the selected value into a hidden field. On form submission, process the value of the hidden field and not the list fields. You may want to compare the value submitted in the hidden field against the submitted list values if you need to know from which list the value was submitted, which is likely.
Choose A Value From One List
<?PHP
If ($_SERVER["REQUEST_METHOD"]=="POST") {
$result = "You chose " . $_POST["theValue"];
// From which list?
If ($_POST["s1"]==$_POST["theValue"]) {
$result .= " from List 1.";
}
If ($_POST["s2"]==$_POST["theValue"]) {
$result .= " from List 2.";
}
}Else{
$result = "Please choose from one list";
}
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="content-language" content="en">
<meta name="author" content="Roderick Divilbiss">
<meta name="copyright" content="© 2005-2010 Roderick Divilbiss">
<title>Submit Value From One List</title>
<script type="text/javascript">
// Don't submit if theValue is empty
function validate() {
if (document.getElementById('theValue').value=='') {
alert('Select a value from one list.');
return false;
}else{
return true;
}
}
</script>
</head>
<body>
<p><?PHP echo $result; ?></p>
<form method="POST" name="frm" action="thispage">
<p><label for="s1">List1: </label><select size="1" name="s1" id="s1" onchange="document.getElementById('theValue').value=this[this.selectedIndex].value;">
<option value="">Select...</option>
<option value="100">100</option>
<option value="200">200</option>
<option value="300">300</option>
<option value="400">400</option>
<option value="500">500</option>
</select>
<label for="s2">List2: </label><select size="1" name="s2" id="s2" onchange="document.getElementById('theValue').value=this[this.selectedIndex].value;">
<option value="">Select...</option>
<option value="Jan">Jan</option>
<option value="Feb">Feb</option>
<option value="Mar">Mar</option>
<option value="Apr">Apr</option>
<option value="May">May</option>
<option value="Jun">Jun</option>
<option value="Jul">Jul</option>
<option value="Aug">Aug</option>
<option value="Sep">Sep</option>
<option value="Oct">Oct</option>
<option value="Nov">Nov</option>
<option value="Dec">Dec</option>
</select><br><br>
<label for="theValue">This would be type hidden, not type text</label>
<input type="text" size="10" name="theValue" id="theValue" value="">
<input type="submit" value="Submit" name="btn"></p>
</form>
<p>
</body>
</html>