Link to home
Start Free TrialLog in
Avatar of Lia Nungaray
Lia NungarayFlag for United States of America

asked on

Populating textboxes upon drop down selection

I finally thought that I had found the answer to my dilema, but not quite yet. I have the following code which populates ONE textbox when the user makes a selection. I actually need TWO textboxes to be populated, but unfortunately, the code that I have doesn't seem do be built to just add a little more code to satisfy this need. Here's the code:

<?php
//Connect to the db.
      $dbc = mysql_connect('localhost','root','');
      mysql_select_db('production',$dbc);

echo "

<html>
<head>
<script language=\"javascript\">
function getCustName()
{
  document.myform.ldgact.value=document.myform.appnbr.value;
}

</script>
</head

<body>
<form method=\"POST\" name=\"myform\" action=\"add_scan.php\">
      <p>Account Number:<input type=\"text\" name=\"ldgact\" size=\"50\"></p>
      <p>Production Date:<input type=\"text\" name=\"datprd\" size=\"10\"></p>>";

      $query="SELECT APPNBR, CUSTNAME, DATPRD FROM tblappearance, tblcustomers WHERE tblappearance.ldgact=tblcustomers.mcustnbr";

      if ($hResult=mysql_query($query))
      {
              while ($hFetch=mysql_fetch_array($hResult))
              {
                $appnbr=$hFetch["APPNBR"];
                $custname=$hFetch["CUSTNAME"];

                echo "<option value='$custname'\">".$appnbr."</option>";
              }
      }

echo "

</select>
</form>
</body>
</html>";

Any suggestions will be welcome. Thanks!
Avatar of Lia Nungaray
Lia Nungaray
Flag of United States of America image

ASKER

Just to clarify, I want DATPRD to be filled in a third text box.
Avatar of BogoJoker
BogoJoker

Hi horalia,

1. I hope that is not your real user/pass and if so you should never run as root.
2. Where do you open your <select>
3. In the javascript part just fill in the other textboxs with the value you want.  (I can't tell what it is you want, I only see 2 textfields)

Joe P
No, it's not. It's password protected, I just erase it whenever I copy some code on the web. I erased the fields just to show what I currently have that works. Here's my complete working code:

<?php
//Connect to the db.
      $dbc = mysql_connect('localhost','root','');
      mysql_select_db('production',$dbc);

echo "

<html>
<head>
<script language=\"javascript\">
function getCustName()
{
  document.myform.custname.value=document.myform.appnbr.value;
}
</script>
</head>

<body>
<form method=\"POST\" name=\"myform\" action=\"add_scan.php\">

<p>
Customer Name:
<input type=\"text\" name=\"custname\" size=\"50\">
</p>

<p>
Production Date:
<input type=\"text\" name=\"datprd\" size=\"10\">
</p>

<p>
Insertion Order Number:
<select size=\"1\" name=\"appnbr\" onChange=\"getCustName()\">";

$query="SELECT APPNBR, CUSTNAME, DATPRD FROM tblappearance, tblcustomers WHERE tblappearance.ldgact=tblcustomers.mcustnbr";

if ($hResult=mysql_query($query))
{
  while ($hFetch=mysql_fetch_array($hResult))
  {
    $appnbr=$hFetch["APPNBR"];
    $custname=$hFetch["CUSTNAME"];
    $datprd=$hFetch["DATPRD"];
    echo "<option value='$custname'\">".$appnbr."</option>";
  }
}

echo "

</select>
</form>
</body>
</html>";

My final purpose is to add some extra fields for the user to fill in and post all this information to the database. I already have the posting part working, this is just a separate file where I'm trying to make the drop-down boxes work, then I will use this file to post the values. Just wanted to let you know if there are some precautions I should take when doing this. Thanks!
All you need to do is pretty much copy this line in your javascript function:
document.myform.ldgact.value = document.myform.appnbr.value;

Change the "ldgact" of the first part to the name of the textfield you want.
If I understand correctly you want two textboxes to have the value of the drop down box.

Joe P
ASKER CERTIFIED SOLUTION
Avatar of Roonaan
Roonaan
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I'll try your suggestions. Thanks!
Thanks Roonan! Worked great!