Note that there are some explanatory texts on larger screens.

plurals
  1. POselectbox with ajax code
    primarykey
    data
    text
    <p>I have 2 selectboxs</p> <p>1.areaID</p> <p>2.cityID</p> <p>When user choose area in the first selectbox, the second one will be auto change and shows only the cities accourding the selected area.</p> <p>that works good, except when I load data from the DB or when user submit the form. in those cases the right city doesn't selected - the pointer is always on the first option (first city).</p> <p>what do I need to change?</p> <pre><code>&lt;p&gt;&lt;label&gt;AREA&lt;/label&gt; &lt;select name='areaID' id='areaID'&gt; &lt;?PHP $query = mysql_query("SELECT * FROM `areas` ORDER BY id ASC "); while($index = mysql_fetch_array($query)) { $db_area_id = $index['id']; $db_area_name = $index['name']; if ($db_area_id == $userDetails['areaID']) echo "&lt;option value='$db_area_id' selected&gt;$db_area_name&lt;/option&gt;"; else echo "&lt;option value='$db_area_id'&gt;$db_area_name&lt;/option&gt;"; } ?&gt; &lt;/select&gt;&lt;span&gt;*&lt;/span&gt; &lt;/p&gt; &lt;p&gt;&lt;label&gt;CITY&lt;/label&gt; &lt;select id='cityID' name='cityID'&gt; &lt;/select&gt; &lt;/p&gt; &lt;script&gt; &lt;?PHP if ($_POST) { ?&gt; $(document).ready(function(){ $('#areaID').filter(function(){ var areaID=$('#areaID').val(); var cityID=&lt;?PHP echo $userDetails['cityID'] ?&gt;; $('#cityID').load('ajax/getCities.php?areaID=' + areaID+'&amp;cityID=' + cityID); return false; }); }); &lt;?PHP }else { ?&gt; $(function () { function updateCitySelectBox() { var areaID = $('#areaID').val(); $('#cityID').load('ajax/getCities.php?areaID=' + areaID); return false; } updateCitySelectBox(); $('#areaID').change(updateCitySelectBox); }); &lt;?PHP } ?&gt; &lt;/script&gt; </code></pre> <p>getCities.php :</p> <pre><code>&lt;?PHP $areaID = (int) $_GET['areaID']; $second_option = ""; $query2 = mysql_query("SELECT * FROM `cities` WHERE area_id = $areaID ORDER BY id ASC"); while($index = mysql_fetch_array($query2)) { $id = $index['id']; $name = $index['name']; $name = iconv('windows-1255', 'UTF-8', $name); $second_option .= "&lt;option value='$id'&gt;$name&lt;/option&gt;"; } echo $second_option; // exit; ?&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload