Xdega Posted October 4, 2010 Share Posted October 4, 2010 Basically atm I am trying to accomplish a way to $_POST a selection from my dropdown box in to a PHP variable for use elsewhere. The eventual goal being a form that will allow you to select a user from the dropdown box; then via a HTML form with some additional text fields, add/update database table records for that person. I have been searching and searching, and I cant find anything that addresses using a dropdown box that pulls names from my SQL in a html web form. All I can find are tons of guides on simple html only forms. At the moment, the dropdown box is working, It is pulling names from my database/table. But when I select a name from the list and then click on my submit button the $_POST does not seem to be pulling the value for "username". I have the following for the index page: <?php include('/nav.php'); include('/config.php'); include('/dbopen.php'); echo "Select a name: <select name='$name'>"; $query="select * from players"; $result=mysql_query($query); while($row=mysql_fetch_array($result)){ $name=$row['PLAYER_NAME']; echo "<option name=username value='$name'>$name</option>"; } echo "</select>"; ?> <html> <body> <form action="moduser.php" method="post" /> <input type="submit" value="CLICK!" /> </form> </body> </html> and the following for a test page to see if my form is being submitted correctly: <?php include('/nav.php'); include('/config.php'); include('/dbopen.php'); $username="$_POST[username]"; echo "Hello $username"; ?> NOTE: My includes are tested and working default scripts to connect to database, define some global vars etc. Should not be relevant to my particular problem Thanks Much, Dega. Quote Link to comment https://forums.phpfreaks.com/topic/215160-help-using-variables-in-html-form/ Share on other sites More sharing options...
WatsonN Posted October 4, 2010 Share Posted October 4, 2010 edit it to your liking <? ... mysql cnx code ... $sql="SELECT id, thing FROM table"; $result=mysql_query($sql); $options=""; while ($row=mysql_fetch_array($result)) { $id=$row["id"]; $thing=$row["thing"]; $options.="<OPTION VALUE=\"$id\">".$thing; } ?> ... html code ... <SELECT NAME=thing> <OPTION VALUE=0>Choose <?=$options?> </SELECT> ... Quote Link to comment https://forums.phpfreaks.com/topic/215160-help-using-variables-in-html-form/#findComment-1119076 Share on other sites More sharing options...
rwwd Posted October 4, 2010 Share Posted October 4, 2010 edit it to your liking <? ... mysql cnx code ... $sql="SELECT id, thing FROM table"; $result=mysql_query($sql); $options=""; while ($row=mysql_fetch_array($result)) { $id=$row["id"]; $thing=$row["thing"]; $options.="<OPTION VALUE=\"$id\">".$thing; } ?> ... html code ... <SELECT NAME=thing> <OPTION VALUE=0>Choose <?=$options?> </SELECT> ... Just be sure to add the 'php' to the <?php or else there are consequences when you use this software on a different server, just means that it's more compatible - just thought I should point that out. In your original example you are building the select box OUTSIDE of the form <> so no wonder it's not getting posted, the only value currently set is $_POST['submit'] and this will have the value of "CLICK!" So as WatsonN points out, build your selection box outside of the form, then echo that var out WITHIN the form tags, then run print_r on the receiver script to see that your DB values are getting through. Rw Quote Link to comment https://forums.phpfreaks.com/topic/215160-help-using-variables-in-html-form/#findComment-1119078 Share on other sites More sharing options...
Xdega Posted October 5, 2010 Author Share Posted October 5, 2010 Thanks, Instead of copying the code, I just modified my current code (better to understand what I am doing). Makes a lot of sense and nailed the issue. Thank you much, understanding this helps me a lot with my project Quote Link to comment https://forums.phpfreaks.com/topic/215160-help-using-variables-in-html-form/#findComment-1119179 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.