CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   JavaScript frameworks (http://www.codingforums.com/forumdisplay.php?f=62)
-   -   Php/ajax - Country/state Chained Select Options (http://www.codingforums.com/showthread.php?t=282403)

quasiman 11-16-2012 09:20 PM

Php/ajax - Country/state Chained Select Options
 
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\" />";
        }
}
?>


quasiman 11-16-2012 11:28 PM

Nevermind, I got it...
Code:

$(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);
                        }
                });
        });
        $(".country").change(function() {  // bind a change event:
                $(this).val();
        }).change(); // and trigger a "change" event immediately
});



All times are GMT +1. The time now is 12:56 PM.

Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.