0

I have made many select box in PHP and I want to keep selected item as selected after refreshing the page. (when selecting same select box or another) here is my code.

$selectbox='<select class="form-control" name="estate_id" onchange="this.form.submit()" style="width: 200px" >';

$est_name = $client ->call('get_estate');  // call method from web services
$_SESSION['estname'] = array();
$_SESSION['estname'] = $est_name;
$count = count($_SESSION['estname']);

$i = 0;
foreach ($_SESSION['estname'] as $row)
 {
    $id   = $_SESSION['estname'][$i]['est_id'];
    $name = $_SESSION['estname'][$i]['est_name'];
    if($id == isset($_POST['estate_id']))
     {
        $isSelected = ' selected="selected"'; 
     } 
     else {
        $isSelected = ''; 
     }
     $selectbox.= "<option value=".$id.$isSelected.">".$name."</option>";
         $i++;
}
$selectbox.='</select>';
echo $selectbox;
Bhumi Shah
  • 9,323
  • 7
  • 63
  • 104
Hasee
  • 7
  • 1
  • 5
  • Possible duplicate of [Keep values selected after form submission](https://stackoverflow.com/questions/2246227/keep-values-selected-after-form-submission) – Masivuye Cokile May 30 '17 at 10:21
  • You'll probably be needing local storage or something. A refresh does not send data(form data) to the server – Jelmergu May 30 '17 at 10:21
  • Would you please put `$isSelected` variable outside option value? and check it. – Purvik Dhorajiya May 30 '17 at 10:23
  • refer https://stackoverflow.com/questions/22125280/how-to-keep-checkbox-checked-after-refresh-the-page – Joseph May 30 '17 at 10:23
  • @Jelmergu ... sort of does, browsers resend the data they had when the page was loaded - hence the pop-up warning about form resubmission in most browsers with POST forms. Doesn't send *new* data, granted. – CD001 May 30 '17 at 10:25
  • @CD001 yes that is if the last request is the submit of a form, but what I think the asker means here is before any submit is done. i.e. I see a form, fill in some fields, refresh the page, I don't need to fill in the fields I already did – Jelmergu May 30 '17 at 11:11

1 Answers1

0
<select name="name">
   <option <?php if ($_GET['name'] == 'a') { ?>selected="true" <?php }; ?>value="a">a</option>
   <option <?php if ($_GET['name'] == 'b') { ?>selected="true" <?php }; ?>value="b">b</option>
</select>
sairam
  • 25
  • 10
  • i solved that my problem in same way what i have done with small changes. thanks for everyone – Hasee May 31 '17 at 05:33