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>

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.