Hello all,
Using php/mysql/ajax, I can't see how to autoselect the state when a country is preselected. See my code below...any help would be appreciated!
ajax:
Code:
<script src="/js/jquery-1.7.2.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function()
{
$(".country").change(function()
{
var dataString = 'id='+ $(this).val();
$.ajax
({
type: "POST",
url: "state.php",
data: dataString,
cache: false,
success: function(html)
{
$(".state").html(html);
}
});
});
});
</script>
fields:
Code:
<tr>
<td><strong>Country</strong></td>
<td><select name="country" class="country">
<option selected="selected">--Select Country--</option>
<?php
$query = "SELECT `id`, `iso`, `country` FROM regions ORDER BY `country`";
$result = $mysqli->query($query) or die($mysqli->error . __LINE__);
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
echo "<option value='" . $row['id'] . "' " . ($customer['country'] == $row['iso'] ? 'selected=\"selected\"' : '') . " >" . $row['country'] . "</option> \n";
}
}
?>
</select></td>
</tr>
state.php
PHP Code:
<?php
include ('includes/functions.php');
if($_POST['id'])
{
$id=$_POST['id'];
$query = "SELECT id,region_id,name FROM subregions WHERE region_id = '$id'";
$result = $mysqli->query($query) or die($mysqli->error . __LINE__);
if ($result->num_rows > 0) {
echo "<select name='state' >\n";
echo "<option value='0'>====Choose State ====</option>\n";
while ($row = $result->fetch_assoc()) {
echo "<option value='" . $row['name'] . "' >" . $row['name'] . "</option> \n";
}
} else {
echo "<input name=\"state\" type=\"text\" name=\"other\" />";
}
}
?>