Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Apr 2009
    Location
    Oregon, USA
    Posts
    10
    Thanks
    6
    Thanked 1 Time in 1 Post

    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\" />";
            }
    }
    ?>
    Last edited by quasiman; 11-16-2012 at 09:22 PM.

  • #2
    New Coder
    Join Date
    Apr 2009
    Location
    Oregon, USA
    Posts
    10
    Thanks
    6
    Thanked 1 Time in 1 Post
    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
    });


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •