The drop down selection should remain as it is after submit

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
anuragl
Forum Newbie
Posts: 1
Joined: Tue Aug 23, 2005 6:07 pm
Contact:

The drop down selection should remain as it is after submit

Post 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]
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
User avatar
harrisonad
Forum Contributor
Posts: 288
Joined: Fri Oct 15, 2004 4:58 am
Location: Philippines
Contact:

Post 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);
Post Reply