How to display the data in the input box retrieved from the dropdown select dynamic?

of illustrations.

  1. How do I take a name and its output appears in a textbox?

  2. and at the same time it appears appropriate selectbox2 id of selectbox1.

  3. The case number 2 is already finished a case number one yet.

here’s code javascript of index.php

$(document).ready(function()
{
	$(".country").change(function()
	{
		var id=$(this).val();
		var dataString = 'id='+ id;
	
		$.ajax
		({
			type: "POST",
			url: "get_state.php",
			data: dataString,
			cache: false,
			success: function(html)
			{
				$(".state").html(html);
			} 
		});
	});

	$(".country").change(function()
	{
		var id=$(this).val();
		var dataString = 'id='+ id;
	
		$.ajax
		({
			type: "POST",
			url: "get_name.php",
			data: dataString,
			cache: false,
			success: function(html)
			{
				$(".country_name").val=html;
			} 
		});
	});
});

here’s code form in index.php:

<select name="country" class="country">
<option selected="selected">--Select Country--</option>
<?php
	$stmt = $DB_con->prepare("SELECT * FROM tbl_country");
	$stmt->execute();
	while($row=$stmt->fetch(PDO::FETCH_ASSOC))
	{
		?>
        <option value="<?php echo $row['country_id']; ?>"><?php echo $row['country_name']; ?></option>
        <?php
	} 
?>
</select>

<label>Nama Negara</label>
<input type="text" name="country_name" class="country_name">

<label>State :</label> <select name="state" class="state">
<option selected="selected">--Select State--</option>
</select>

here’s code of get_name.php

<?php
include('dbconfig.php');
if($_POST['id'])
{
	$id=$_POST['id'];
		
	$stmt = $DB_con->prepare("SELECT country_name FROM tbl_country WHERE country_id=:id");
	$stmt->execute(array(':id' => $id));
	while($row=$stmt->fetch(PDO::FETCH_ASSOC))
	{
		 echo $row['country_name']; 
	}
}
?>

Please help me, thank you.

Wouldn’t you just combine the two things into the success() function? I don’t know much about Ajax, but I doubt you can do it the way you have - just have a single .change() trap, and put the return anywhere you need it. If you need to return more than one value, look at JSON-encoding it, then splitting back into separate fields on return.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.