php_beginner_83 Posted July 31, 2009 Share Posted July 31, 2009 Hi All On my webpage I want to use a pull down menu. This menu I want to be populated with values from a table in my mysql database. However, what I want to do is have my pull down menu have a bound column like in Microsoft Access. So for example, say I have two sets of values from the table in my database, ID and NAME. In my pull down menu, I want to display the NAME values but actually want to use the ID value associated with the NAME value. It's the ID value I need but if I display this in the pull down menu, the user will have no clue what it means. Does anyone have any suggestions or advice about how this can be done?? Thank you. Quote Link to comment Share on other sites More sharing options...
rhodesa Posted July 31, 2009 Share Posted July 31, 2009 <?php $sql = 'SELECT id,name FROM someTable'; $result = mysql_query($sql) or die(mysql_error()); echo '<select name="some_field">'; while($row = mysql_fetch_assoc($result)){ printf('<option value="%s">%s</option>',htmlspecialchars($row['id']),htmlspecialchars($row['name'])); } echo '</select>'; ?> Quote Link to comment Share on other sites More sharing options...
php_beginner_83 Posted July 31, 2009 Author Share Posted July 31, 2009 Will that display the NAME values in the pull down menu but actually give me the ID value? I want to put this in a <form action='upload.php' method='post'>....</form> and when I press the submit button all values from the form can be used to add a record to a database. So even though the NAME value is displayed, the ID value is what will be passed to my upload.php code for further processing. Thanks for your help. Quote Link to comment Share on other sites More sharing options...
roopurt18 Posted July 31, 2009 Share Posted July 31, 2009 Why not try it and see? Part of programming is typing code, running code, and examining the output. You took the time to type out a well-thought question. rhodesa took the time to type out a well-thought response. You then immediately come back and ask him if it will do what you asked in the original post. This implies that: 1) You did not try it at all 2) You don't think rhodesa has the reading comprehension to understand your original post 3) (or maybe) You think your OP wasn't that clear after all. Quote Link to comment Share on other sites More sharing options...
php_beginner_83 Posted July 31, 2009 Author Share Posted July 31, 2009 Yes I did try it. And it displays a pull down menu with the values from my table. Excuse me for wanting some further explanation. If I knew what I was doing I wouldn't be asking for help. Quote Link to comment Share on other sites More sharing options...
roopurt18 Posted July 31, 2009 Share Posted July 31, 2009 Yes I did try it. And it displays a pull down menu with the values from my table. Did you look at the raw HTML that was produced? Did you submit the form and examine $_POST? That would have answered your question. Quote Link to comment Share on other sites More sharing options...
rhodesa Posted July 31, 2009 Share Posted July 31, 2009 Yes, the ID will get passed as the value. If you want to test it, put this at the top of the PHP page it's submitting to: <?php print '<pre>'.print_r($_POST).'</pre>'; exit; ?> That will print the array of all the data being submitted by the form Quote Link to comment Share on other sites More sharing options...
php_beginner_83 Posted July 31, 2009 Author Share Posted July 31, 2009 Like I said roopurt if I knew what I was doing I wouldn't be asking for help. Perhaps my screen name would of given it away....php_BEGINNER_83. I'm glad however to see that your idea of posting in a forum is to berate and belittle someone who is genuinely trying to get help to solve a problem. rhodesa, thanks for the help. I'm trying to learn php on my own so I really appreciate it when more experienced programmers take the time to constructively offer solutions. Quote Link to comment Share on other sites More sharing options...
roopurt18 Posted July 31, 2009 Share Posted July 31, 2009 If you want to know what is being output by PHP it's not sufficient to examine the output as rendered by a browser. You need to use the 'View source' feature of whichever browser you're using to view the actual HTML source PHP sent to the browser. Had you done that, you would have seen: <select name="some_field"> <option value="1">Joe</option> <option value="2">Larry</option> <option value="3">John</option> <option value="4">Betty</option> </select> Now if you don't understand what that output means, there's a fine XHTML reference over at w3schools: http://www.w3schools.com/tags/default.asp http://www.w3schools.com/tags/tag_select.asp http://www.w3schools.com/tags/tag_option.asp If you understand that the text within value="" is what is sent back to PHP when the form is submitted, then it's pretty clear IDs are sent back while NAMEs are displayed. The intent of my posts was not to offend you, but to let you know there was more you could have done to check if the solution was correct yourself. Using PHP to create HTML that will then be rendered in a browser is a pretty core concept to the language; so is using the View source feature of the browser to examine the output. Now since you were articulate and asking a question most likely related to forms, I had assumed you knew how to do those things. Heck, you were even asking about a bound control, which is what other languages sometimes call such a feature. Anyways I was wrong and my apologies for offending you. 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.