python72 Posted November 12, 2010 Share Posted November 12, 2010 I am brand new to php and I don't know where I am going wrong with this. I would like to have drop down menu which is populated from mySQL and which would allow user to select one option. The selection would be used on the following page once the form is submitted. Right now I have the following script and it populates the dropdown menu but it does not return value when selection is made. <form action="databaseinputcheck2.php" method="post"> <?php mysql_connect("localhost", "xxxxxxxxxxxx", "xxxxxxxxxxxxxx") or die(mysql_error()); echo "Connected to MySQL<br />"; ?> <p>Lotteries: <?php mysql_select_db("wpawlowski") or die(mysql_error()); // Get all the data from the "LotteryNames" table $lotterynameresult = mysql_query("SELECT id, Name FROM LotteryNames") or die(mysql_error()); ?> <select name="LotteryName" size="1"> <option selected value="">Select One</option> <option value="">----------</option> <?php // keeps getting the next row until there are no more to get while($currentlottery = mysql_fetch_array( $lotterynameresult )) { $lid=$currentlottery['id']; $lname=$currentlottery['Name']; // Print out the contents of each row into drop down menu echo "<option value='$lid'>$lname</option>\n"; //$options.="<OPTION VALUE=\"$lid\">".$lname; //Selection input line } ?> <input type="submit" value="GO" /> </form> I looked on the internet and it is my understanding that if I comment out the line starting withc "echo" and uncomment the one below the drop down menu should still be populated and the selection would be stored in the $options variable which in turn could be transfered to databaseinputcheck2.php with $_POST to be used there, but when I do uncomment that line and comment the echo line out my drop down menu is not populated, what am I doing wrong? Quote Link to comment https://forums.phpfreaks.com/topic/218510-drop-down-menu-input-help-needed/ Share on other sites More sharing options...
Vitamin Posted November 12, 2010 Share Posted November 12, 2010 while($currentlottery = mysql_fetch_array( $lotterynameresult )) { $lid=$currentlottery['id']; $lname=$currentlottery['Name']; echo '<option value="' . $lid . '">' . $lname . '</option>'; } Give that a try. Quote Link to comment https://forums.phpfreaks.com/topic/218510-drop-down-menu-input-help-needed/#findComment-1133560 Share on other sites More sharing options...
python72 Posted November 13, 2010 Author Share Posted November 13, 2010 The echo command does populate my drop down menu but which variable would store the selection? Quote Link to comment https://forums.phpfreaks.com/topic/218510-drop-down-menu-input-help-needed/#findComment-1133678 Share on other sites More sharing options...
Vitamin Posted November 13, 2010 Share Posted November 13, 2010 $lname is the value in the dropdown box (the actual displayed value to the user) $lid is the value of the selected option I think that is what you are asking. Quote Link to comment https://forums.phpfreaks.com/topic/218510-drop-down-menu-input-help-needed/#findComment-1133679 Share on other sites More sharing options...
python72 Posted November 13, 2010 Author Share Posted November 13, 2010 So if I include the following code in databaseinputcheck2.php it should display the lid value once the GO button is clicked? <?php $lid=$_POST['lid']; echo "lid: $lid"; ?> Right now it does not work, is there more code I have to include to transfer this value to the next form? Quote Link to comment https://forums.phpfreaks.com/topic/218510-drop-down-menu-input-help-needed/#findComment-1133685 Share on other sites More sharing options...
Vitamin Posted November 13, 2010 Share Posted November 13, 2010 You named it LotteryName so you would want to do <?php $lid=$_POST['LotteryName']; echo "lid: $lid"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/218510-drop-down-menu-input-help-needed/#findComment-1133688 Share on other sites More sharing options...
python72 Posted November 13, 2010 Author Share Posted November 13, 2010 That works, thanks a lot for your help Vitamin Can you direct me to good source on php like tutorial, etc? I am getting lost with all the slashes, single quotes, double quotes, dots etc. Quote Link to comment https://forums.phpfreaks.com/topic/218510-drop-down-menu-input-help-needed/#findComment-1133691 Share on other sites More sharing options...
Vitamin Posted November 13, 2010 Share Posted November 13, 2010 Never really read a tutorial that about those sorts of things, but the best resource around is http://www.php.net. In all honesty your problem was somewhat HTML based. The best advice anyone ever gave me when I started to learn php was to echo variables all the time when debugging to make sure they are what you are expecting them to be. Oh and make sure to mark your problem solved. (A button on the bottom left hand of this screen) Quote Link to comment https://forums.phpfreaks.com/topic/218510-drop-down-menu-input-help-needed/#findComment-1133693 Share on other sites More sharing options...
python72 Posted November 13, 2010 Author Share Posted November 13, 2010 Is there a way to refresh webpage every time selection in drop down menu is changed? This way values could be echoed every time selection is changed. Quote Link to comment https://forums.phpfreaks.com/topic/218510-drop-down-menu-input-help-needed/#findComment-1133696 Share on other sites More sharing options...
Vitamin Posted November 13, 2010 Share Posted November 13, 2010 Yes, but you don't really want to refresh the page. You want to use java script to change the values. Quote Link to comment https://forums.phpfreaks.com/topic/218510-drop-down-menu-input-help-needed/#findComment-1133697 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.