Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Feb 2004
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Populate textbox, when user clicks items from listbox(Onchange)

    Hi,

    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)){
    }
    else
    {
    echo ('No Results!');
    }

    if($row = mysql_num_rows($result)){
    ?>
    <tr>
    <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";

    $SName="";
    $Code="";

    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>";

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

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

  • #2
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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
    PHP Code:
    ...
    $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
    Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html

  • #3
    Senior Coder Nightfire's Avatar
    Join Date
    Jun 2002
    Posts
    4,265
    Thanks
    6
    Thanked 48 Times in 48 Posts
    You can't. Not without having to refresh everytime, but even then you'd still need javascript to do the onchange.

    BTW:
    PHP Code:
    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 </edit>


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •