prcollin Posted July 28, 2008 Share Posted July 28, 2008 Ok i have a drop down list that I want to update everytime a new client is added to my database so that when i want to view a client i can just clik on the drop down and choose the client. Question is how do i code a drop down to autopopulate with the first and last name of the clients that are in the database in real time. $client_fname $client_lname are the column headings. I want it to be last name, first name int he drop down menu. Thanks ahead of time Quote Link to comment https://forums.phpfreaks.com/topic/116921-using-php-to-autopopulate-a-drop-down-menu/ Share on other sites More sharing options...
Pyro Posted July 28, 2008 Share Posted July 28, 2008 $member_list = ""; while($row = mysql_fetch_array( $result )) { if($row['name'] == $_POST['name']){ $member_list .= "<option selected=\"selected\" value=\"".$row['name']."\">".$row['name']."</option>"; }else{ $member_list .= "<option value=\"".$row['name']."\">".$row['name']."</option>"; } } thats the php for one of my member drop downs, and here is the form: <td width="175" align="center" class="cell"><select name="name" class="entryfield" id="name"> <option value="">Select.</option> <?php echo $member_list; ?> </select> </td> there is a lot more than that, but that is the basic's of it, im sure you will be able to fix somthing out of that. ~Py Quote Link to comment https://forums.phpfreaks.com/topic/116921-using-php-to-autopopulate-a-drop-down-menu/#findComment-601259 Share on other sites More sharing options...
prcollin Posted July 28, 2008 Author Share Posted July 28, 2008 $member_list = ""; while($row = mysql_fetch_array( $result )) { if($row['name'] == $_POST['name']){ $member_list .= "<option selected=\"selected\" value=\"".$row['name']."\">".$row['name']."</option>"; }else{ $member_list .= "<option value=\"".$row['name']."\">".$row['name']."</option>"; } } thats the php for one of my member drop downs, and here is the form: <td width="175" align="center" class="cell"><select name="name" class="entryfield" id="name"> <option value="">Select.</option> <?php echo $member_list; ?> </select> </td> there is a lot more than that, but that is the basic's of it, im sure you will be able to fix somthing out of that. ~Py ty very much Quote Link to comment https://forums.phpfreaks.com/topic/116921-using-php-to-autopopulate-a-drop-down-menu/#findComment-601269 Share on other sites More sharing options...
Pyro Posted July 28, 2008 Share Posted July 28, 2008 No Problem it should work. you may have to tweek it to your spec, but i know it works on mine. Quote Link to comment https://forums.phpfreaks.com/topic/116921-using-php-to-autopopulate-a-drop-down-menu/#findComment-601272 Share on other sites More sharing options...
prcollin Posted July 29, 2008 Author Share Posted July 29, 2008 No Problem it should work. you may have to tweek it to your spec, but i know it works on mine. I have tweaked it as much as possible and even tried a new code, and I cannot get my drop down list to autopoulate. Any other suggestions this is what I am using now. <?php include "clientconnect.php"; mysql_select_db('greencut_customercenter', $con); if ($result = mysql_query("SELECT * FROM clients")) { if (mysql_num_rows($result)) { echo "<form>"; echo " <select name=\"clients\">"; while($row = mysql_fetch_array($result)) { echo " <option value=\"{$row['client_lname']}\">{$row['client_fname']} {$row['client_lname']}</option>"; } echo " </select>"; echo "</form>"; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/116921-using-php-to-autopopulate-a-drop-down-menu/#findComment-602661 Share on other sites More sharing options...
emmy Posted October 27, 2012 Share Posted October 27, 2012 (edited) I am currently working on something like this and was able to achieve the autopulation of my drop down list, the only difference here is that my database is postgresql while yours is mysql but there is not much difference in the syntax. Here are the things I notice odd in your script. echo " </select>"; The </select>"; should be outside the PHP tags I will simply post this dropdown script for you here, if you dont mind taking a look at it and manipoulate yours to get it to function. It pretty easy Edited October 27, 2012 by emmy Quote Link to comment https://forums.phpfreaks.com/topic/116921-using-php-to-autopopulate-a-drop-down-menu/#findComment-1388153 Share on other sites More sharing options...
emmy Posted October 27, 2012 Share Posted October 27, 2012 (edited) sorry i am not able to post the code. Im having problem with the toggle here, its messing up codes. Edited October 27, 2012 by emmy Quote Link to comment https://forums.phpfreaks.com/topic/116921-using-php-to-autopopulate-a-drop-down-menu/#findComment-1388154 Share on other sites More sharing options...
Psycho Posted October 27, 2012 Share Posted October 27, 2012 Here are the things I notice odd in your script. echo " </select>"; The </select>"; should be outside the PHP tags That statement is incorrect. There is no reason any HTML code has to be in or outside the PHP tags as long as it is being properly formatted based on the context and the desired output. I see specifically nothing wrong with the code the OP last posted. But, since he didn't post what the output is that he is getting there's no way to know what the problem is. Quote Link to comment https://forums.phpfreaks.com/topic/116921-using-php-to-autopopulate-a-drop-down-menu/#findComment-1388156 Share on other sites More sharing options...
emmy Posted October 27, 2012 Share Posted October 27, 2012 Well! I am not an expert in this but I am telling you from my own current personal experience my script did not populate until i did that. Here is a sample of my script that did the job. I think you can look at and try and try it out. It took me days to figure out that the problem had to do with the placement of my </select> tag <html> <head><title>Title</title></title></head> <body> <!--HTML Code to create the data entry form--> <p></P> <form action="fieldtally.php" method="post"> <table width="600" cellpadding= "10" cellspacing="1" border="2"> <tr align="center" valign="top"> <td align="center" colspan="1" rowspan="1" bgcolor="#64b1ff"> <h3></h3> <select pipeno="pipeno" name="pipeno"><br><br> <?php //Php Code to connect to postgresqldatabase include ("connection.php"); // Code to pull data from the database and load onto the form echo "<br />\n"; $query = 'select pipeno, wallthickness from fieldtally order by pipeno asc '; $result =mysql_query($db_handle,$query); while ($row = mysql_fetch_row($result)) { $pipeno = $row[0]; echo "<option>$pipeno</option>"; } ?> </select> </form> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/116921-using-php-to-autopopulate-a-drop-down-menu/#findComment-1388160 Share on other sites More sharing options...
Jessica Posted October 27, 2012 Share Posted October 27, 2012 That statement is incorrect. There is no reason any HTML code has to be in or outside the PHP tags as long as it is being properly formatted based on the context and the desired output. I see specifically nothing wrong with the code the OP last posted. But, since he didn't post what the output is that he is getting there's no way to know what the problem is. QFT. Quote Link to comment https://forums.phpfreaks.com/topic/116921-using-php-to-autopopulate-a-drop-down-menu/#findComment-1388164 Share on other sites More sharing options...
Psycho Posted October 27, 2012 Share Posted October 27, 2012 Well! I am not an expert in this but I am telling you from my own current personal experience my script did not populate until i did that. Here is a sample of my script that did the job. I think you can look at and try and try it out. It took me days to figure out that the problem had to do with the placement of my </select> tag Well, I hate to tell you but your "personal experience" must be based upon a problem which you were not aware of. Whatever fixed your code had nothing to do about whether the select tag was echo'd within the PHP code or whether it was directly within HTML code. It very well may have been due to incorrectly echoing it in the wrong place. When usign code to generate other code it can be difficult to keep track of everything. But, I can categorically state that you can output the select tags within the PHP code to achieve the same results. The two blocks of code will result in the same output: <select name='field_name'> <?php $options = array('1' => 'one', '2' => 'two', '3' => 'three'); foreach($options as $id => $value) { echo "<option value='{$id}'>{$value}</option>\n"; } ?> </select> <?php echo "<select name='field_name'>"; $options = array('1' => 'one', '2' => 'two', '3' => 'three'); foreach($options as $id => $value) { echo "<option value='{$id}'>{$value}</option>\n"; } echo "</select>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/116921-using-php-to-autopopulate-a-drop-down-menu/#findComment-1388171 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.