Page 1 of 1

The drop down selection should remain as it is after submit

Posted: Tue Aug 23, 2005 6:34 pm
by anuragl
Hello everyone,

Ive written a simple code in which a person has to select the range for two factors and click submit. a value is associated with the two values. Their sum is printed. My requirement is after the submit is clicked, the range that was selected for two questions should be displayed as it is instead of displaying the default selection. can someone help me on this.

Here is the code

Code: Select all

<html>
<head>


</head>
<body>

<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">

</p>
<p>&nbsp;</p>

What is the range of ABC 

factor<select name="value1">
<option value=2>Low
<option value=5>Medium
<option value=10>High
</select></div>
</p>

</p>
<p>&nbsp;</p>


What is the range of DEF

factor
<select name="value2">
<option value=2>Low
<option value=5>Medium
<option value=10>High
</select>

</p>
<p>&nbsp;</p>



<input type="submit" name="submit" value="Submit">



</p>
<p>&nbsp;</p>

</form>

</body>
</html>


<?php

if (isset($_POST['submit']))
 {

$v1= $_POST["value1"];
$v2= $_POST["value2"];
$v5 = $v1 + $v2;

echo "According to the selections that you have made the value is $v5"; 
}

?>

feyd | Please use

Code: Select all

and

Code: Select all

tags where approriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]

Posted: Tue Aug 23, 2005 6:46 pm
by feyd
typically, one would compare the value posted with that in the current output for the option, writing the "selected" marking when the values match.

Since your options are hardcoded, you will have to write a check for each option line, but the idea is still the same.

Posted: Tue Aug 23, 2005 7:54 pm
by harrisonad
Put the task of creating your selectboxes in a function with $sel_option as one of the arguements.

Code: Select all

function BuildSelectBox($sel_option,$name,$options) // $options is an array
{
    // output select box, loop through options
    // if current options is sel_option then put 'SELECTED'
}
Then when you get the $_POST variable for your select box value, call the function with that value

Code: Select all

$value = $_POST['selectbox'];
BuildSelectBox($value,'selectbox',$options);