Sunday, 12 October 2014

PHP with Ajax Data


<!-- test.php page -->
<script type="text/javascript" src="jquery.js"></script>
<?php
    $con = mysql_connect("localhost","root","");
    mysql_select_db("demo", $con);
    error_reporting(1);
?>



<table>
<tr>
    <td>Country:</td>
    <td>
        <select name="country_id" id="country_id" style="width:140px;" onchange="getTestState(this.value);">
            <option value="">-- Select --</option>
            <?php
                $sql = "Select * from country where status='1'";
                $sql1 = mysql_query($sql);
                while($result = mysql_fetch_assoc($sql1)){ ?>
                <option value="<?php echo $result["id"]; ?>"><?php echo $result["country_name"]; ?></option>
            <?php } ?>
        </select>
    </td>&nbsp;
</tr>
<tr id="state">
</tr>
</table>


<script type="text/javaScript">
    function getTestState(id){
        $.ajax({
            type: "POST",
            url: "get_test_state.php",
            data: "country_id="+id,
            beforeSend:function(){
            //alert(categoryId);
            },
            success: function(response){
                //alert(response); return false;
                    $('#state').html(response);
            }
        });
    }
</script>




<!-- responce page -->
<?php
include("include/config.php");
$country_id = $_POST['country_id'];
$sql = "SELECT id,state_name from state where country_id='".$country_id."'";
$sql1 = mysql_query($sql);
?>
    <td>State:</td>
    <td>
        <select name="state_id" id="state_id" style="width:140px;">
            <option value="">-- Select --</option>
            <?php while($result = mysql_fetch_assoc($sql1)){ ?>
                <option value="<?php echo $result["id"]; ?>"><?php echo $result["state_name"]; ?></option>
            <?php } ?>
        </select>
    </td>

No comments:

Post a Comment