Hi everyone

I have a list box called tc_id
I have populated this list box with values from teh database.
Now i want to fetch values from the database into two text boxes based on the selection made in the listbox.

Recommended Answers

All 10 Replies

Try something like the following:

<?
if (isset($_POST['tc_id'])) {
$result=mysql_query("SELECT * FROM `table` WHERE `column`='".mysql_real_escape_string($_POST['tc_id'])."'");
$row=mysql_fetch_array($result);
echo "<input type='text' value='".$row['column']."'><br>";
echo "<input type='text' value='".$row['column1']."'>";
}
?>
<select name='tc_id'>
Your existing select box here...

Hi Cwarn23 thanks for your reply

It worked but what i wanted was as sson as the selection in the
listbox is made the value would be fetch into the textboxes. Right now i have put up a button besides the listbox and when the user clicks it the values are fetched into the textboxes.

Try adding the following to your form and the select objects.

<form name='forma1'>
<select onChange="document.forms['forma1'].submit();">

With the name in the form and that onChange event it will make the form submit when the select value changes.

Hey that didnt work. I am posting my code. If you could suggest some changes.Thanks for your help

<table align="center" border="0" cellspacing="1" cellpadding="0">

 <form name="tc_slot_form" method="POST" action="tc_slot1.php">
 <font face='verdana' size='2'>
  <tr>
    <td width="129"></td>
    <td colspan="2" valign="middle"></td>
  </tr>
  <tr>
    <td></td>
    <td colspan="2" valign="middle"></td>
  </tr>
  <tr>
    <td ><div align="left">Test Center ID: </div></td>
    <td colspan="2" valign="middle"><label>
      <select onchange="document.tc_slot_form.submit()" name="tc_id">
	  <?php
	  	$sql_tc_id="select tc_id from tc_details0709";
		
		$result_tc_id=mysql_query($sql_tc_id);
		if(!$result_tc_id)
		{
			echo("Error retrieving from database !!!");
			exit();
		}
	   while($row_id = mysql_fetch_array($result_tc_id))
		{
			$tc_id_val = $row_id['tc_id'];
			echo "<option value=$tc_id_val>$tc_id_val</option>";
		}
	  	if (isset($_POST['tc_id']))
		{
			$result=mysql_query("SELECT * FROM tc_details0709 WHERE tc_id='".mysql_real_escape_string($_POST['tc_id'])."'");
			$row=mysql_fetch_array($result);
		}		
		?>

     </select>
    </label>
      <label>
      <input type="submit" name="Submit" value="get values" />
      </label>
      <br/>
        <label></label>
       <label></label>
      <br/></td>
  </tr>
  <tr>
    <td></td>
    <td colspan="2"></td>
  </tr>
  <tr>
    <td><div align="left">Test Center City: </div></td>
	<td colspan="2" valign="middle"><br/>
      <input name="tc_city" type="text" size="20" readonly="true" value="<?php echo $row['tc_city'];?>"><br></td>
  </tr>
  <tr>
    <td></td>
    <td colspan="2"></td>
  </tr>
  <tr>
    <td ><div align="left">Test Center Address:</div></td>
    <td colspan="2"><br/>
        <textarea name="tc_addr" cols="60" rows="3" readonly="readonly" ><?php echo $row['tc_addr']; ?></textarea><br /></td>
  </tr>
  
  <tr>
    <td colspan="3"></td>
  </tr>
  <tr>
    <td><div align="left">Time Slot: </div></td>
    <td colspan="2" valign="middle"><br/>
        <input name="tc_time_slot" type="text" size="45" /> 
        (eg. 10:00 am to 11:00 am) </td>
  </tr>
  <tr>
    <td colspan="3">&nbsp;</td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>
     
        
        <p align="center">&nbsp;</p>		</td>
    <td width="336"><p align="center"><input type="submit" value="Submit" name="submit" /></p></td>
    <td width="136">&nbsp;</td>
  </tr>
  </font>
  <br/>
 <tr><td colspan="3"><div align="center"></div>
    <font size="2" face="verdana">
      <?php
		if($submit)
		{
			$sql_count="select count(*) as tot_time_slot from tc_time_slot0709";
			$result_get_count=mysql_query($sql_count);
			if(!$result_get_count)
			{
				echo("Error retrieving from database !!!");
				exit();
			}
			if($row_count = mysql_fetch_array($result_get_count))
			{
				$app_no = $row_count['tot_time_slot'];
			}
			$allo_id_no = 10 + $app_no;
			$aloc_id = $tc_id . $allo_id_no;
			
		    $sql_insert_tc_slot = "insert into tc_time_slot0709 (aloc_id, tc_id, tc_time_slot) values ('$aloc_id','$tc_id_val','$tc_time_slot')";
			
			$result_insert=mysql_query($sql_insert_tc_slot);
			echo "Records Updated Time slot added Allocation id is: $aloc_id";
			}
	?>
       </font></td>
 </tr>
 <font face='verdana' size='2'>	</font>
  </form>
</table>

I spot a php bug in there and you also did the onchange event wrong. So the code should be as follows:

<table align="center" border="0" cellspacing="1" cellpadding="0">

 <form name="tc_slot_form" method="POST" action="tc_slot1.php">
 <font face='verdana' size='2'>
  <tr>
    <td width="129"></td>
    <td colspan="2" valign="middle"></td>
  </tr>
  <tr>
    <td></td>
    <td colspan="2" valign="middle"></td>
  </tr>
  <tr>
    <td ><div align="left">Test Center ID: </div></td>
    <td colspan="2" valign="middle"><label>
      <select onchange="document.forms['tc_slot_form'].submit();" name="tc_id">
	  <?php
	  	$sql_tc_id="select tc_id from tc_details0709";
		
		$result_tc_id=mysql_query($sql_tc_id);
		if(!$result_tc_id || mysql_num_rows($result_tc_id)==0)
		{
			echo("Error retrieving from database !!!");
			exit();
		}
	   while($row_id = mysql_fetch_array($result_tc_id))
		{
			$tc_id_val = $row_id['tc_id'];
			echo "<option value=$tc_id_val>$tc_id_val</option>";
		}
	  	if (isset($_POST['tc_id']))
		{
			$result=mysql_query("SELECT * FROM tc_details0709 WHERE tc_id='".mysql_real_escape_string($_POST['tc_id'])."'");
			$row=mysql_fetch_array($result);
		}		
		?>

     </select>
    </label>
      <label>
      <input type="submit" name="Submit" value="get values" />
      </label>
      <br/>
        <label></label>
       <label></label>
      <br/></td>
  </tr>
  <tr>
    <td></td>
    <td colspan="2"></td>
  </tr>
  <tr>
    <td><div align="left">Test Center City: </div></td>
	<td colspan="2" valign="middle"><br/>
      <input name="tc_city" type="text" size="20" readonly="true" value="<?php echo $row['tc_city'];?>"><br></td>
  </tr>
  <tr>
    <td></td>
    <td colspan="2"></td>
  </tr>
  <tr>
    <td ><div align="left">Test Center Address:</div></td>
    <td colspan="2"><br/>
        <textarea name="tc_addr" cols="60" rows="3" readonly="readonly" ><?php echo $row['tc_addr']; ?></textarea><br /></td>
  </tr>
  
  <tr>
    <td colspan="3"></td>
  </tr>
  <tr>
    <td><div align="left">Time Slot: </div></td>
    <td colspan="2" valign="middle"><br/>
        <input name="tc_time_slot" type="text" size="45" /> 
        (eg. 10:00 am to 11:00 am) </td>
  </tr>
  <tr>
    <td colspan="3">&nbsp;</td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>
     
        
        <p align="center">&nbsp;</p>		</td>
    <td width="336"><p align="center"><input type="submit" value="Submit" name="submit" /></p></td>
    <td width="136">&nbsp;</td>
  </tr>
  </font>
  <br/>
 <tr><td colspan="3"><div align="center"></div>
    <font size="2" face="verdana">
      <?php
		if($submit)
		{
			$sql_count="select count(*) as tot_time_slot from tc_time_slot0709";
			$result_get_count=mysql_query($sql_count);
			if(!$result_get_count)
			{
				echo("Error retrieving from database !!!");
				exit();
			}
			if($row_count = mysql_fetch_array($result_get_count))
			{
				$app_no = $row_count['tot_time_slot'];
			}
			$allo_id_no = 10 + $app_no;
			$aloc_id = $tc_id . $allo_id_no;
			
		    $sql_insert_tc_slot = "insert into tc_time_slot0709 (aloc_id, tc_id, tc_time_slot) values ('$aloc_id','$tc_id_val','$tc_time_slot')";
			
			$result_insert=mysql_query($sql_insert_tc_slot);
			echo "Records Updated Time slot added Allocation id is: $aloc_id";
			}
	?>
       </font></td>
 </tr>
 <font face='verdana' size='2'>	</font>
  </form>
</table>

i copied the entire code but nothing changed.
The form does not submit and i get an error in IE saying the object does not support this property

Try making a html file with the following html code as a test.

<form name='tc_slot_form' method='get'>
<select onchange="document.forms['tc_slot_form'].submit();" name="tc_id">
<option value=1>test
<option value=2>test2
</select></form>

Then when you load that html file and select an option from the menu, does the url then have a variable in it (example: tc_id=2) or does it just report the error. If it just reports an error then it's probably because of something like activex being disabled or something like that.

that works if i allow the blocked content.

But then how do i retain the selected value and show it back again.
Because what i want it

After the selection is made the two text boxes should get values from the database based on the selection in the listbox.

Then there is a third text box that the user would fill in once he see the three values the listbox selection made and the values in the two text boxes.

After that he submits the form.

Thanks for you help

I know this sounds ugly/old but what about if each select box was in an iframe and you just set the form target to the next iframe. So the page it redirects to will just contain the processor and the new menu. That will prevent the whole page from refreshing. Other than that you could use the script I gave you earlier that would allow for one menu to make another.

i have done it another way now

I take the id i.e the listbox on another page.
and rest of the fields on another page.

the client has no prob with it, so its fine now.

Thanks for you replies

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.