JustinWaters Posted February 25, 2011 Share Posted February 25, 2011 So I've been attempting to create a dropdown list that will post to mySQL. The problem I'm running into is the way I populate the dropdown I can't figure out how to return the selected option to mySQL. <form name="pentry" method="post" action="" action="pentry.php"> <input name="username" type="hidden" value="<?php echo "$username"?>"> Date of Practice:<input type="text" id="dates" name="practdate" datepicker="true" datepicker_format="YYYY-MM-DD" maxlength="100"><br> <?php $query="SELECT eoptions,id FROM sm_options"; /* You can add order by clause to the sql statement if the names are to be displayed in alphabetical order */ $result = mysql_query ($query); echo "<select name=eoptions value=''>Event Options</option>"; // printing the list box select command while($nt=mysql_fetch_array($result)){//Array or records stored in $nt echo "<option value=$nt[id]>$nt[eoptions]</option>"; /* Option values are added by looping through the array */ } echo "</select>";// Closing of list box mysql_close(); ?><br> Event:<input type="text" name="event" maxlength="100" value="<?php echo "test $output" ?>"><br> Session Time:<input type="text" name="practtime" id="practtime">min. <input type="button" value=" + " onClick="addmin(practtime);"> <input type="button" value=" - " onClick="submin(practtime);"><br> Practice Content:<input type="text" name="practnotes"><br> <input type="submit" name="submit" value="Submit"><br> </form> I've been moving code around trying to solve the problem so its a bit messy. Quote Link to comment Share on other sites More sharing options...
Psycho Posted February 25, 2011 Share Posted February 25, 2011 You can't "... return the selected option to mySQL". You need to have a form that POSTs to a PHP page. That PHP page will take the POSTed values (perform any necessary validations) and run a query to insert the values. Assuming your select list is being generated and you can select a value, have you determined if the value is being passed to the processing page? There are some minor issues with your existng code that may or may not be part of your problem. 1) You are not enclosing the "vlaue" of the options in quotes. If the values have a space this will cause a problem. 2) Your select statement is malformed. It is always preferable, in my opinion, to generate your dynamic code in the beginning of your script and then produce all the output at the end - instead of trying to put php code within the output portion of your code. Makes it much easier to see those types of errors and gives you a lot of flexibility. <?php // Create and run query to get list of event options $query = "SELECT eoptions,id FROM sm_options"; $result = mysql_query($query); // Loop through results to create the options list for events $eventOptions = ''; while($nt=mysql_fetch_array($result)) { /* Option values are added by looping through the array */ $eventOptions .= "<option value=\"{$nt['id']}\">{$nt['eoptions']}</option>\n"; } mysql_close(); ?> <form name="pentry" method="post" action="" action="pentry.php"> <input name="username" type="hidden" value="<?php echo $username; ?>"> Date of Practice: <input type="text" id="dates" name="practdate" datepicker="true" datepicker_format="YYYY-MM-DD" maxlength="100"> <br> <select name="eoptions">Event Options</option> <option value="">Event Options</option> <?php echo $eventOptions; ?> </select> <br> Event: <input type="text" name="event" maxlength="100" value="<?php echo "test $output" ?>"> <br> Session Time: <input type="text" name="practtime" id="practtime">min. <input type="button" value=" + " onClick="addmin(practtime);"> <input type="button" value=" - " onClick="submin(practtime);"> <br> Practice Content: <input type="text" name="practnotes"><br> <input type="submit" name="submit" value="Submit"><br> </form> Quote Link to comment Share on other sites More sharing options...
JustinWaters Posted February 25, 2011 Author Share Posted February 25, 2011 I went through the code you did. After understanding it I found 1 line which was creating redundancy of empty dropdown lists. I removed that line. I'm trying to figure out how with the new code how to submit the selection with the <form> <?php // Create and run query to get list of event options $query="SELECT eoptions,id FROM sm_options"; $result = mysql_query ($query); // Loop through results to create the options list for events $eventOptions = ''; while($nt=mysql_fetch_array($result)) { // printing the list box select command /* Option values are added by looping through the array */ $eventOptions .= "<option value=\"{$nt['id']}\">{$nt['eoptions']}</option>\n"; } mysql_close(); ?> <form name="pentry" method="post" action="pentry.php"> <input name="username" type="hidden" value="<?php echo "$username"?>"> Date of Practice:<input type="text" id="dates" name="practdate" datepicker="true" datepicker_format="YYYY-MM-DD" maxlength="100"><br> <select name="eoptions">Event Options</option><?php echo $eventOptions; ?> </select><br> Session Time:<input type="text" name="practtime" id="practtime">min. <input type="button" value=" + " onClick="addmin(practtime);"> <input type="button" value=" - " onClick="submin(practtime);"><br> Practice Content:<input type="text" name="practnotes"><br> <input type="submit" name="submit" value="Submit"><br> </form> Appearantly there is also an unclosed </Select> floating around not sure where at though. Normally Dreamweaver recgonizes when it has closed a tag. But I can do </ many times and it always thinks it needs to close another </select>...anyways kinda besides the point Quote Link to comment Share on other sites More sharing options...
Psycho Posted February 25, 2011 Share Posted February 25, 2011 I'm trying to figure out how with the new code how to submit the selection with the <form> Not sure I understand your confusion. You have a form which contains a select list. Select a value from the list and click the submit button. Your processing page, "pentry.php", will need to access that POSTed value ($_POST['eoptions']) and do something with it. Quote Link to comment Share on other sites More sharing options...
JustinWaters Posted February 25, 2011 Author Share Posted February 25, 2011 Appearantly you can't edit your posts so here is some additional Information. Database Columns: ID | practdate | timestamp | username | event | practtime | practnotes Row: 1 | 2011-02-16 |2011-02-23 14:32:46 | JustinWaters | Practice | 15 | Music All information is being recorded except the event 'Practice'. They used to have to type it in but since attempting to integrate dropdown list it hasn't worked. Returning blank. Edit: Ah ok I'll check that. Thank You Quote Link to comment Share on other sites More sharing options...
JustinWaters Posted February 25, 2011 Author Share Posted February 25, 2011 eoptions returns number value(1-6) not the name(Practice-Sectional). 1 - Practice 6 - Sectional memberspage.php <?php // Create and run query to get list of event options $query="SELECT eoptions,id FROM sm_options"; $result = mysql_query ($query); // Loop through results to create the options list for events $eventOptions = ''; while($nt=mysql_fetch_array($result)) { // printing the list box select command /* Option values are added by looping through the array */ $eventOptions .= "<option value=\"{$nt['id']}\">{$nt['eoptions']}</option>\n"; } mysql_close(); ?> <form name="pentry" method="post" action="pentry.php"> <input name="username" type="hidden" value="<?php echo "$username"?>"> Date of Practice:<input type="text" id="dates" name="practdate" datepicker="true" datepicker_format="YYYY-MM-DD" maxlength="100"><br> <select id="event" name="eoptions">Event Options</option><?php echo $eventOptions; ?> </select><br> Session Time:<input type="text" name="practtime" id="practtime">min. <input type="button" value=" + " onClick="addmin(practtime);"> <input type="button" value=" - " onClick="submin(practtime);"><br> Practice Content:<input type="text" name="practnotes"><br> <input type="submit" name="submit" value="Submit"><br> </form> pentry.php //Collect Form Data $username = $_POST['username']; $practdate = $_POST['practdate']; $event = $_POST['eoptions']; $practtime = $_POST['practtime']; $practnotes = $_POST['practnotes']; // Query for existing user $checkuser = mysql_query("SELECT sm_pchart FROM mission"); $query = "INSERT INTO sm_pchart (practdate, username, event, practtime, practnotes) VALUES('$practdate', '$username', '$event', '$practtime', '$practnotes')"; mysql_query($query) or die(mysql_error()); mysql_close(); Quote Link to comment Share on other sites More sharing options...
Psycho Posted February 25, 2011 Share Posted February 25, 2011 eoptions returns number value(1-6) not the name(Practice-Sectional). 1 - Practice 6 - Sectional That is what you want. You have the eoptions in thier own table, so you want to store the ID from the sm_options table into the mission table. That is how you should reference data between table (using a foreign ID). Then when you pull data from the mission table you do a JOIN on the sm_options table to get the textual value. With the above process if you were to ever change a description in the sm_options ns table it applies to all the records that use that option. If you try to store the textual value in the mission table you woul dhave to update all the records when you want/need to make a change So, in your mission table, I would change the "event" column name to "sm_options_id". Then store the numerical value in that column. You can then get the values from the mission table along with the textual value for the option using a JOIN query Example: SELECT m.*, sm.eoptions FROM mission AS m JOIN sm_options AS sm ON m.sm_options_id = sm.id Quote Link to comment Share on other sites More sharing options...
JustinWaters Posted February 25, 2011 Author Share Posted February 25, 2011 Well then, ... ... ... Thanks for the insight! I think that is problem solved I probably should change it from pulling Username to UserID then!!! This opens alot of posibilies 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.