Jump to content

Auto populate textfield via dropdown box


zed420

Recommended Posts

Hi All

I hope someone can help me out its been long 24hrs, What I'm trying to do is auto populate the foreign key with help of dropdown box that contains names of clients so if i select a name from dropdown box a textfield above should populate with it's ID(primary key). I'll appreciate some please.

Thanks

Zed

 

    <td>Client ID : </td>
    <td><input type="text" id="cl_id" name="cl_id" value="<?
$query = "SELECT * FROM client ";
$result = mysql_query($query)or die(mysql_error());
    while ($row = mysql_fetch_array($result))  {
    extract($row);
    echo "
    " . $row['client_id'] . "
   ";
   }
?>" size="3" disabled /></td></tr>
    <td>Company Name : </td>
    <td><select name="clientName" class="text" onchange="document.getElementById('cl_id').value=this.option  s[this.selectedIndex].value;">
<option value="">Select</option><?
$query = "SELECT * FROM client ";
$result = mysql_query($query)or die(mysql_error());
    while ($row = mysql_fetch_array($result))  {
    extract($row);
    echo "
    <option>" . $row['name'] . "</option>
   ";
   }
?></select></td></tr>

When requesting help for JavaScript do not include PHP code. Post the HTML from the processed code - makes it much easier.

 

There's nothing wrong with your javascript, except that you have several spaces in the middle of the action. But, your code is very disorganized. I see a lot of possible errors also. For example you use extract() on the record, but then use $row['client_id'] to get the value - why use extract then? Your process for creating the options list is not populating a value for the options so the javascript can't populate a value that does not exist.

 

Try something like this

<?php
    //Get current client id
    $query = "SELECT * FROM client ";
    $result = mysql_query($query)or die(mysql_error());
    $record = mysql_fetch_assoc($result));
    $client_id = $record['client_id'];

    //Create option list of clients
    $options = "<option value=\"\">Select</option>";
    $query = "SELECT * FROM client ";
    $result = mysql_query($query)or die(mysql_error());
    while ($record = mysql_fetch_assoc($result))
    {
        $selected = ($record['id']==$client_id) ? ' selected="selected"' : '';
        $options .= "<option value=\"{$record['id']}\"{$selected}>{$record['name']}</option>\n";
    }
?>

  <tr>
    <td>Client ID : </td>
    <td><input type="text" id="cl_id" name="cl_id" value="<?php echo $client_id; ?>" size="3" disabled /></td>
  </tr>
  <tr>
    <td>Company Name : </td>
    <td>
      <select name="clientName" class="text" onchange="document.getElementById('cl_id').value=this.options[this.selectedIndex].value;">
      <?php echo $options; ?>
      </select>
    </td>
  </tr>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.