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.

Recommended Answers

All 4 Replies

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.

Hi,

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.

<?php
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!
$x=1;
## 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
while($x<=4)
  {
  echo "<option> " . $x . "</option>";
  $x++;
  }
  echo '</select><input type="submit" name="option_one" value="Query"></form></td>';
  
  if($_POST['option_one']){
  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>';
  
  }
 elseif($_POST['option2']){
  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>';
  
  
  } 

  else{
  
  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());
mysql_select_db("hotel");


$hotelid=$_GET;


?>
<html>
<head>
<SCRIPT LANGUAGE="JavaScript">
<!-- Beginning of JavaScript -
function OnChange(dropdown)
{


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



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



<?php


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



?>


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


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


</script>
<?php


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


?>


</select>
<?php
$query="SELECT * FROM tblroomregistration  where _roomid='$f1'";
$result=mysql_query($query);
while ($row = mysql_fetch_array($result))
{
$roomtype=$row;
$type2 = $row;
$cost = $row;


?>



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



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


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

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.