Hello to all,
I have a problem that how can i fetch value from database when i select dropdown list option related information should be show in textbox in php.
Value should be retrieve from database.
In dropdown list values are also coming from database.

5 Years
Discussion Span
Last Post by abhinav1986

You would need to use JavaScript/Ajax to run your php script on selection from the dropdown list and then populate the textbox with the query results


Ya AJAX is the answer, follow these steps.

1, learn the JavaScript that calls an X ML and prepares itself to receive information from the server
2, define where you'd like the response to be displayed
3, Call your php page(server side script) using GET and the url with the variables set
4, Code your php page so it delivers a response, 'the one you want'.
5, Define in the body of your page the div you call in step 2.

Edited by Metophase: n/a



Since, you have not provided any codes to begin with, why not try the simulator first and then once you are familiar with how the things are processed, you can write similar script that applies to your situation.

I am not sure if there is something you can learn from this, but for whatever it's worth. here you go,

save as whatever.php, upload to your server.. tweak it as much as you can, and then attempt to use the actual query from database.

echo '<table cellpadding="4" cellspacing="4"><tr><td><form method="post" action=""><select name="option1">';
## strangely enough lets make $x equal to your query, duh!
## below represents the query results from db
## we have 4 different items to choose from.
## each value of $x has a matching value on $y of which  another group of query based on the selected value of $x
  echo "<option> " . $x . "</option>";
  echo '</select><input type="submit" name="option_one" value="Query"></form></td>';
  echo '<td valign="top">  Result of Query One-->  </td>';
  ## first we need to provide the result based on the selected option above
  ## just to simulate the result lets put another form here
  echo '<td><form method="post" action=""><select name="result1">';
  $y = $_POST['option1'];
  ## portion below is the modeled interpretation of a query from the database, such as
  ## SELECT" FROM something WHERE y ='" . $x . "' LIMIT 1
  ## now we provide values for selected $x above
  ## Array below represents columns in mysql and the numerical values represents id or any columns that can be use to sort
  $array = array( 'Show more options for one' => 1,  'Show more options for Two' => 2, 'Show more options for Three' => 3, 'Show more options for Four ' =>4);
  ## array search represents to query like SELECT" FROM something WHERE y ='" . $x . "' LIMIT 1
 $show_y = array_search($y, $array); 
  ## below is the mathcing result if there is something to be printed.
  echo '<option value="'. $y .'">'. $show_y .'<option>' ;
  echo '</select>';
  echo '<input type="submit" name="option2" value="Show options for '. $y .'"/>';
  echo '</form></td>';
  echo '<td valign="top">  Result of Query Two -->  </td>';
  ## codes below are nothing but a repeat of the above 
  ## just in case we want to display another select based on the value of $y
  echo '<td><form method="post" action=""><select name="result2">';
  ## we can still use $y here, because it is protected by the condition elseif. otherwise, it will create problems
  $y = $_POST['result1'];
  ## once again the array and the $show_y could represent your mysql query for the third level select option
   $array = array( 'I love being number one' => 1,  'Second is not bad' => 2, 'Third is at least before the fourth ' => 3, 'It is lonely down here ' =>4);
   $show_y = array_search($y, $array); 
  echo '<option value="'. $y .'">'. $show_y .'<option>' ;
  echo '</select>';
  echo '<input type="submit" name="option3" value="Show more options for '. $show_y .'"/>';
  echo '</form></td></tr>';

  echo '<tr><td colspan="4"> No Query Result for query three! Sorry I ran out of selections for ya , please start all over again at Query one.</td></tr></table>';

<?php mysql_connect("localhost","root","") or die(mysql_error());


<!-- Beginning of JavaScript -
function OnChange(dropdown)

var myindex  = dropdown.selectedIndex;
var SelValue = dropdown.options[myindex].value;
var baseURL  = SelValue;

return true;
<td> <select name="select1" onChange="OnChange(this.form.select1);" id="category">


$query="SELECT _roomid FROM tblroomregistration where _hotelid='$hotelid' order by _hotelid asc";
while ($row = mysql_fetch_array($result))


<option value="<?php echo $f1; ?>" > <?php echo $f1; }?></option>

<script type="text/javascript">
var f2= OnChange(this.form.select1);


$f3= "<script language='javascript'>
document.write( f2);
echo $f3;


$query="SELECT * FROM tblroomregistration  where _roomid='$f1'";
while ($row = mysql_fetch_array($result))
$type2 = $row;
$cost = $row;


<td>Room Type </td>
<td><input type="text" name="rtype"  value="<?php  echo $roomtype; ?>"   size="20">

<td><input type="text" name="roomtype" value="<?php echo $type2; ?>"  size="20"/></td>

<td>Total Cost</td>
<td><input type="text" name="cost" value="<?php echo $cost; }?>" size="20"/></td>

This is the part of my code.But i did not get any idea about which java script code i should use.I am getting value in dropdown list. but i am not getting relative value in textbox.

Edited by Nick Evan: Fixed formatting

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.