tags:

views:

40

answers:

4

hi everybody i have a problem please help me i have 4 select tag in my php page and when i post my parameters to my page all of select tags clear and i want my selected item dont change - excuse me for my bad english

A: 

Use $_SESSION
http://php.net/manual/en/features.sessions.php

Oyeme
How will that help?
David Dorward
All selected tags will be stored in session'sThen check it - 'selected' or not selected.
Oyeme
strictly, this doesn't really answer the question
Chaim Chaikin
Nor is it a good way of doing it. What's the point of storing the submitted value in a session just to read the value back from the session?
icio
Session will store your choose, for a short time.You can jump from one page to another with same domaina name.
Oyeme
+1  A: 

When outputting the option elements, check if the submitted value matches the value you are are adding and add a selected attribute.

e.g.

$test = $_POST['name_of_select'];  // This should be outside the loop
$v = htmlspecialchars($data['value'],ENT_QUOTES);
$d = htmlspecialchars($data['display_value'],ENT_QUOTES);
$c = "";
if ($data['value'] === $test) {
    $c = " checked";
}
echo "<option value='$v'$c>$d</option>";
David Dorward
A: 

Here's a pretty way to do it in your views

<select name="select">
<?php foreach( $options as $option ): ?>
    <option value="<?php echo $option ?>"<?php if($_POST['select'] == $option):?> checked="checked"<?php endif; ?>>
<?php endforeach; ?>
</select>

This loops through your options and checks the value of the select each time. If the value of the select is the same as the value of the option it checks that specific option.

Galen
A: 

Let the javascript do it automatically for you:

<select name="myname" id="myid">
  .....
  ...options...
  .....
</select>

Below your select box, put javascript code:

<script type="text/javascript">
  document.getElementById('myid').value = '<?php echo $_POST["myname"];?>';
</script>

and so on for other select boxes.

Note: This works only in best-case scenarios, you should actually do it in PHP only.

Sarfraz
Why on earth would you depend on JavaScript for this, when you get a better user experience (that won't break is JS is off) doing it server side? Oh, and there's an XSS hole in that example too.
David Dorward
@David Dorward: yup agreed and actually i forgot to mention that it is not a solution in case javascript is disabled or used in a bad fashion. Thanks
Sarfraz