How to Get Content in text field dynamically, based on dropdown selection in php

I have two fields in database, one is university_name and another one is department_name. I am Displaying university_name in drop down using while loop. When customer selects university_name from dropdown then i want to show its department(department_name) in another text field. Please help me.

my php code:

<div id="main-container">
<label>University:</label> 
<select name="university" id="university">
<option selected="selected">--Select University--</option>
<?php
include_once 'dbconfig.php';
	$stmt = $DB_con->prepare("SELECT DISTINCT university_name FROM filter");
	$stmt->execute();
	while($row=$stmt->fetch(PDO::FETCH_ASSOC))
	{
	$university = $row['university_name'];
	 $_SESSION["university_name"] = $university;
		?>
     <option value="<?php echo $row['university_name']; ?>"><?php echo $row['university_name']; ?></option>
        <?php
	} 
?>
</select>
<br>
<label>Department :</label> <input type="text" name="department" id="department">
</div>

The way I would do this is to use JavaScript, trigger a function when the user selected the university from the first drop-down, have that function call a separate bit of PHP code to retrieve all the departments for that university and stick them in the second drop-down.

If you have a search down the forum, I put some sample code in another thread. ETA this one: How to auto-fill 2 drop-down and text-box based upon on the first drop-down value selected? - #7 by droopsnoot

The down-side of this is that if you have to deal with users that have JavaScript switched off, that approach won’t work.

i have tried the above method but it not working

Well, show us the code and tell us exactly what happens for you. The post I pointed to only has pseudo-code for the secondary menu selection code, so maybe the issue is there.

if i select university from university field,it will automatically shown the department without dropdown option (note:it will shows the multiple departments,but in my project only one department is there so i dont want dropdown option)

So, you only want it to show the html select if there is more than one department, otherwise it just shows a fixed value? I’d imagine you’d then have to have the code that recovers the departments do a row count, and instead of just populating the option lines, it would need to decide to output the entire select or just a text display.

yes, i want to display like text .i am struggling to display as text so please help me

if($_POST[‘id’])
{
$id=$_POST[‘id’];

$stmt = $DB_con->prepare("SELECT DISTINCT  department_name FROM filter WHERE university_name=:id");
$stmt->execute(array(':id' => $id));
?><?php
while($row=$stmt->fetch(PDO::FETCH_ASSOC))
{
	?>
    	<option value="<?php echo $row['department_name']; ?>"><?php echo $row['department_name']; ?></option>
    <?php
}

}

If you just want to display it as text, why do you loop through multiple results and output html option tags? When there is only one option available, you need to just display it in a paragraph tag or whatever you would normally use to display a text item. You will also need to change the calling code to replace not just the option tags, but the entire select div.

i am beginner for php so i am not understand it please post some sample code to display like text field

If you’re sure there will only be one record, you can make the code much simpler:

$stmt = $DB_con->prepare("SELECT DISTINCT  department_name FROM filter WHERE university_name=:id");
$stmt->execute(array(':id' => $id));
?><?php
if ($row=$stmt->fetch(PDO::FETCH_ASSOC))  // change to if to just get one row
{
	?>
    	<p><?php echo $row['department_name']; ?></p>
    <?php
}

What you need to do after that, is change the code in the calling page so that it replaces the correct div. I don’t know what names you are using in that - in my sample code in the post I linked to, I called it “subcats”. So on this line in the calling code:

$("#subcat-list").html(result); // insert in div above

it would need to be

$("#subcats").html(result);

to replace the entire select range, otherwise you’ll be putting a <p> inside a <select> and I’m not sure how well that will work. It also only outputs it as a text display, it does not put it into a form field. If you need it to come into the next page with your other form fields, then you could probably output it as a hidden form field as well as the straight text - that’s just html and duplicating the code above.

Obviously what you need to do after that is change the calling code so that it can work with both scenarios - one department, or more than one. So the pseudo-code for that would be something like

// run the query
// get the number of rows returned
// if it's only one : 
//    retrieve the one row and output it as a paragraph, or para and hidden input 
// otherwise it must be more than one:  **
//    output the opening select tag
//    loop through the results and output them as option tags
//    output the closing select tag

** assuming that you couldn’t have a university in the first drop-down that has no departments.

I’ll leave you to code that, feel free to come back and post your code if you have trouble with it.

sorry i tried that method,but still its not working for me . here i have attached index.php(phpindex.php (2.9 KB)
get_state.php (364 Bytes)
) and get_state.php(ajax) file

please help me

You still loop through the results, though, rather than just getting a single result as in the sample code in post #10 above.

Exactly what do you mean by “still not working”?

i have used p tag inside select tag i don’t display more than one departments in my project. its sample code

Still this question, though.

it will not shows the department value

So, does the query retrieve it correctly in get_state, but the main code not display it? Or is the query failing?

but i have use in select tag it will shows the department

I’m sorry, I don’t know what you mean by that. Is it valid html to put a <p> tag inside a <select>?

ok thanks

Please use an ajax to display the department of your university. eg:

<select name="university" onchange="get_dept(this.value)" id="university">
</select>
Script Code:
function get_dept(univ_name){
$.ajax({
method:"POST",
url:"get_dept.php",
data:{"univ_name":univ_name},
success:function(data){
if(data!='0'){
$('#department').val(data);
}else{
alert('Department not available');
}
}
});
}


ajax file **get_dept.php**:
if(isset($_POST['univ_name'])){
$sql = "SELECT * FROM table_name WHERE university_name='".$_POST['univ_name']."'";
$res = mysql_query($sql);
$result = mysql_fetch_array($res);
if(!empty($result)){
$data = $result[0]['department_name'];
}else{
$data='0';
}
}else{
$data='0';
}