zed420 Posted February 11, 2009 Share Posted February 11, 2009 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> Quote Link to comment Share on other sites More sharing options...
Psycho Posted February 11, 2009 Share Posted February 11, 2009 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> Quote Link to comment Share on other sites More sharing options...
zed420 Posted February 12, 2009 Author Share Posted February 12, 2009 mjdamato that was just brilliant with slight modification I managed to get it going the I wanted it. Much appreciation Zed Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.