Go Back   CodingForums.com > :: Client side development > JavaScript programming > JavaScript frameworks

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-16-2012, 09:20 PM   PM User | #1
quasiman
New Coder

 
Join Date: Apr 2009
Location: Oregon, USA
Posts: 10
Thanks: 6
Thanked 1 Time in 1 Post
quasiman is an unknown quantity at this point
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..
quasiman is offline   Reply With Quote
Old 11-16-2012, 11:28 PM   PM User | #2
quasiman
New Coder

 
Join Date: Apr 2009
Location: Oregon, USA
Posts: 10
Thanks: 6
Thanked 1 Time in 1 Post
quasiman is an unknown quantity at this point
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
});
quasiman is offline   Reply With Quote
Reply

Bookmarks

Tags
ajax, country, mysql, php, state

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:25 AM.


Advertisement
Log in to turn off these ads.