View Full Version : Populate textbox, when user clicks items from listbox(Onchange)

03-26-2004, 01:38 PM

I have a listbox called LstSchoolName, onChange of the item in the listbox, I want to display, that School details in my textbox. I can do this in javascript, but we are not allowed to use javascript. How can I call an onchange event in php?

$sql = "select * from Table1 where SchoolName like '%".$SchoolName."%' order by SchoolName";
$result = mysql_query($sql);

if($row = mysql_num_rows($result)){
echo ('No Results!');

if($row = mysql_num_rows($result)){
<td width="2%">Name</td>
<td width="48%"><input type="text" name="txtSchool" size="37" <?if (!empty($SName)){?> value="<?=$SName?>" <?}?> readonly="true"></td>
echo "<TD rowspan=7 height=\"120\"><SELECT NAME=\"LstSchoolName\" ID=\"LstSchoolName\" SIZE=\"8\">\n";


while ( $row = mysql_fetch_array($result) )

If ($SName!=$row["SchoolName"])
$SName = $row["SchoolName"];
$County = $row["Locality"];
$Street = $row["Street"];
echo "\n<OPTION VALUE='".$row["SchoolName"]."'>".$SName."</OPTION>";

<td width="2%">Address </td>
<td width="48%"><input type="text" name="txtAddr" size="37" <?if (!empty( $Street)){?> value="<?= $Street?>" <?}?> readonly="true"></td>

<td width="2%">County </td>
<td width="48%"><input type="text" name="txtCounty" <?if (!empty( $County )){?> value="<?= $County ?>" <?}?> size="37" readonly="true"></td>

03-26-2004, 02:33 PM
You can't.

As i tryed to explain to you before, once the output is sent to the client (the browser presumably) then you can't execute any PHP on it.

To trap the onchange, you need javascript.

The only thing you can do is put a submitbutton next to the dropdown.
Then post the form to itself, proces the value from the dropdown, change the value for the textbox with PHP and reload the form.
So the form needs to go back to the server to change the textfield.
so inside the formtag, your action needs to be
echo ('<form action="' . $_SERVER['PHP_SELF'] . '" ...>');
and inside the php page:
echo '<input type=text name="bla" value="' . $_POST['dropdown'] . '">';

so on first load, or if no value isselected, the textfield is empty. If a value if selected from the dropdown and the form is posted, then the textfield will contain the selected value.

Or, you first need to run a select, based on the selected alue and then show the details. Like

$sql="select var from table where shcoolID =" . $_POST['dropdown'] ;
echo '<input type=text name="bla" value="' . $row['shooldetails'] . '">';

But an onchange isn't possible (unless you would try some other clientside scripting (clientside vb-script or so) but i think the objective is to make it truely universal, so you need the submitbutton and server side processing

03-26-2004, 02:34 PM
You can't. Not without having to refresh everytime, but even then you'd still need javascript to do the onchange.


if($row = mysql_num_rows($result))

Will always return true, you need to check if it equals 1 or above (>=1)
<edit>I'm getting slower, hi raf :p</edit>