提交后,依赖选择消失[关闭]

I have a searching form that shows a list.

In the form there is a dependant select for the cities (when a country is selected the select for the cities is shown).

The problem: when I submit the form, the page is refreshed so the select for the cities disappear.. What should I do to mantain that select after pushing submit? should I submit the form using ajax?

make a checking like if($_POST["county"]!=""){"load your cities drop down. and city drop down will be filtered by county drop down."} . thinks it will help you.

You need to check the post or get value of the select box and set SELECTED for that specific value:

<?php @$selected[$_GET['test']] = 'SELECTED'; ?>
<form>
  <select name="test">
    <option value="a" <?=@$selected['a']?>>1</option>
    <option value="b" <?=@$selected['b']?>>2</option>
    <option value="c" <?=@$selected['c']?>>3</option>
    <option value="d" <?=@$selected['d']?>>4</option>
  </select>
  <input type="submit" />
</form>

Select 1 -> Country

Select 2 -> Cities For a Country. I assume options are loaded using AJAX

You can select the option from the Select 1 using $_POST and using selected="selected", It's not possible to select the Select 2 values even though they exist in the $_POST.

You will have to use AJAX form submit for this.

First you need to save the value from the list, let's say it's $_POST['city']. Then, if you're on the same page and re-generating the same list of cities, assuming code similar to:

foreach ($cities as $city)
{
    echo "<option value='{$city}'>{$city}</option>";
}

Change it to:

foreach ($cities as $city)
{
    echo "<option value='{$city}'";
    if ($city == $_POST['city']) { echo ' selected="yes"'; }
    echo ">{$city}</option>";
}

If we're not talking about the same page, then as soon as you get $_POST['city'], save it to a session variable, for example $_SESSION['city'], then use as above. This can also be extended to countries, etc.