0

i have a little form with a select list, and i am submitting the form every time i select an item. the problem is that, after selecting a value and submitting the form, the selected item doesn't remain selected. Is there any method to keep the selected item selected in the list, after the form submit?

<form name="order_of_products_by_values" id="order_of_products_by_values" method="post" action="">
<select id="order_of_products_by_values" name="order_of_products_by_values" onChange="this.form.submit();">
<option value=1 >Pret crescator</option>
<option value=2 >Pret descrescator</option>
<option value=3 >Test</option>
<option value=4 >Test</option>
</select>
</form>

please help me out

4
Contributors
7
Replies
12
Views
5 Years
Discussion Span
Last Post by almostbob
0

So, if you write $_POST['order_of_products_by_values'] you don't get anything? if you select Pret crescator you should get 1.

Ops, I see that form and select are using the same id and name. Each element can use only a unique id per page, each form element can share the same name only if they are grouping, like radio buttons, so change the name and the id of your form.

Edited by cereal: icode added

0

wrapping the code in [code=language] bla bla bla [/code] tags to enable highlighting,
and fixed a few html errors, red is not a 'good' color for code, values must be quoted

<form name="order_of_products_by_values" id="order_of_products_by_values" method="post" action="">
<select id="order_of_products_by_values" name="order_of_products_by_values" onChange="this.form.submit();"> 
<option value=1 >Pret crescator</option>
<option value=2 >Pret descrescator</option>
<option value=3 >Test</option>
<option value=4 >Test</option>
</select>
</form>

becomes

<form name="order_of_products_by_values" id="order_of_products_by_values" method="post" action="<?php echo $_server['php_self']; ?>">
<select id="order_of_products_by_values1" name="order_of_products_by_values1" onChange="this.form.submit();"> 
<option value='1' <?php if($_POST['order_of_products_by_values1']=='1') echo 'selected="selected"';?> >Pret crescator</option>
<option value='2' <?php if($_POST['order_of_products_by_values1']=='2') echo 'selected="selected"';?>>Pret descrescator</option>
<option value='3' <?php if($_POST['order_of_products_by_values1']=='3') echo 'selected="selected"';?>>Test</option>
<option value='4' <?php if($_POST['order_of_products_by_values1']=='4') echo 'selected="selected"';?>>Test</option>
</select>
</form>

not clean code but effective,
and didnt bother to code out possible errors at the initial form state where no value has been submitted
eg: <option value='3' <?php if($_post) {if($_POST['order_of_products_by_values1']=='3') echo 'selected="selected"';} ?> >Test</option> leaving you something to do :)

Edited by almostbob: n/a

0

Hi , this is the my exact code:

I passing in while loop:

print "<select name=\"name\">";
print "<option value=''>Select your manager</option>";
while($row2=mysql_fetch_assoc($result1))
{
    
	print "<option value=".$row2['mid'].">".$row2['mngname']."</option>";
}
print "</select>";

hi please give the solutions for this code..what posted now ? thanks

0
echo '<select name="name"><option value="">Select your manager</option>';
while($row2=mysql_fetch_assoc($result1)) {
 echo '<option value="'.$row2['mid'].'"'; //line broken in the middle of the option declaration
 if($row2['mid']= $_POST['name']) echo 'selected="selected"'; //select the appropriate value only
 echo '>'.$row2['mngname'].'</option>'; }//option declaration completed
echo "</select>";

echo is infinitessimally faster than print :)

Edited by almostbob: n/a

0

hi, thanks a lot .Its working prefectly.thanks for your code . For me its very difficult to understand single quotes concatenation...

0

(The previous two posts came in while I was editing this, but maybe it will still be useful to you.)

@jacksantho,

Your code is looking better but you haven't integrated what @almostbob demonstrated. You must echo out some html, selected="selected" for the option that is selected for the entry. What does $result1 contain? For the moment I'll assume it contains a list of all the managers that meet some criteria.

So if these managers are being associated to an employee and we assume that $employee_managers contains an array of this employee's manager IDs:

/**
 *  @param mixed $needle - Item we are searching for
 *  @param array $haystack - values to search through
 */
function isSelected($needle, $haystack) {
  return in_array(trim($needle),$haystack);
}

print "<select name=\"name\" multiple=\"multiple\">";
print "<option value=''>Select your manager</option>";
while($row2=mysql_fetch_assoc($result1))
{
 
	print "<option value=".$row2['mid']." ".(isSelected($row2['mid'], $employee_managers) ? 'selected="SELECTED"' : '').">".$row2['mngname']."</option>";
}
print "</select>";

Edited by madCoder: Post was marked solved while I was writing, so added a short comment.

0

they call it concatenation,,
ever tried to get a cat to do something it didnt already want to ?? same thing :P

Edited by almostbob: n/a

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.