msebar Posted August 16, 2014 Share Posted August 16, 2014 Can someone help here. I can't get the drop down to populate before the post. It does populate after post and the selected option doesn't post after it get populated. Here is the code I have Once I get everything working I will be moving to mysqli or pod <?PHP require_once('../../lib/connections/db.php'); include('../../lib/functions/functions.php'); checkLogin('2'); $getuser = getUserRecords($_SESSION['user_id']); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Water Analysis Data(WAD)</title> <style type="text/css"> </style> </head> <body> <div align="right"><a href="../wad/index.php">Home</a> | <? if (!empty($getuser[0]['thumb_path'])){echo "<a href='.//manage_photo.php'>Manage My Photo</a> | ";} else {echo "<a href='../upload_photo.php'>Upload Photo</a> | ";} ?><a href="../change_pass.php">Change password</a> | <a href="../edit_profile.php">Edit Profile</a> | <a href="../log_off.php?action=logoff">Sign Out</a></div></td> <p>Welcome <?php if(empty($getuser[0]['first_name']) || empty($getuser[0]['last_name'])){echo $getuser[0]['username'];} else {echo $getuser[0]['first_name']." ".$getuser[0]['last_name'];} ?></p> <br><br><br> <?php // this is processed when the form is submitted // back on to this page (POST METHOD) if ($_SERVER['REQUEST_METHOD'] == "POST") { $usernow = $getuser[0]['username']; $userid = $usernow; # escape data and set variables $tank = addslashes($_POST["tank"]); $date = addslashes($_POST["date"]); $temperature = addslashes($_POST["temperature"]); $ph = addslashes($_POST["ph"]); $ammonia = addslashes($_POST["ammonia"]); $nitrite = addslashes($_POST["nitrite"]); $nitrate = addslashes($_POST["nitrate"]); $phosphate = addslashes($_POST["phosphate"]); $gh = addslashes($_POST["gh"]); $kh = addslashes($_POST["kh"]); $iron = addslashes($_POST["iron"]); $potassium = addslashes($_POST["potassium"]); $notes = addslashes($_POST["notes"]); // build query // # setup SQL statement $sql = " INSERT INTO water_parameters "; $sql .= " (id, userid, tank, date, temperature, ph, ammonia, nitrite, nitrate, phosphate, gh, kh, iron, potassium, notes) VALUES "; $sql .= " ('', '$userid', '$tank', '$date', '$temperature', '$ph', '$ammonia', '$nitrite', '$nitrate', '$phosphate', '$gh', '$kh', '$iron', '$potassium', '$notes') "; // #execute SQL statement $result = mysql_query($sql); // # check for error if (mysql_error()) { print "Database ERROR: " . mysql_error(); } print "<h3><font color=red>New Water Parameters Were Added</font></h3>"; } ?> <table width="810" border="2" align="center"> <tr> <td> <table width="800" border="0" align="center" cellpadding="0" cellspacing="0"> <tr> <td align="center" bgcolor="#FFFFFF" scope="col"> <h2><b>Water Analysis Data(WAD)</b></h2></td> </tr> <tr> <td bgcolor="#FFFFFF"> <form name="water_parameters" action="water_parameters.php" method="POST"> <table border="2" align="center" cellpadding="0"> <tr><td><div align="left"><b>Tank Name: </b> </div></td><td><div align="left"> <?php echo "<select>"; $results = mysql_query("SELECT tank FROM tank WHERE userid = '$userid'"); while($row = mysql_fetch_array($results)) { echo "<option value=". $row["tank"] .">". $row["tank"] . "</option>"; } echo "</select>"; ?> </div></td></tr> <tr><td><div align="left"><b>Test Date: </b> </div></td><td><div align="left"> <input type="text" name="date" size=25> </div></td></tr> <tr><td><div align="left"><b>Temperature: </b> </div></td><td><div align="left"> <input type="text" name="temperature" size=25> </div></td></tr> <tr><td><div align="left"><b>pH: </b> </div></td><td><div align="left"> <input type="text" name="ph" size=25> </div></td></tr> <tr><td><div align="left"><b>Ammonia: </b> </div></td><td><div align="left"> <input type="text" name="ammonia" size=25> </div></td></tr> <tr><td><div align="left"><b>Nitrite: </b> </div></td><td><div align="left"> <input type="text" name="nitrite" size=25> </div></td></tr> <tr><td><div align="left"><b>Nitrate: </b> </div></td><td><div align="left"> <input type="text" name="nitrate" size=25> </div></td></tr> <tr><td><div align="left"><b>phosphate: </b> </div></td><td><div align="left"> <input type="text" name="phosphate" size=25> </div></td></tr> <tr><td><div align="left"><b>GH: </b> </div></td><td><div align="left"> <input type="text" name="gh" size=25> </div></td></tr> <tr><td><div align="left"><b>KH: </b> </div></td><td><div align="left"> <input type="text" name="kh" size=25> </div></td></tr> <tr><td><div align="left"><b>Iron: </b> </div></td><td><div align="left"> <input type="text" name="iron" size=25> </div></td></tr> <tr><td><div align="left"><b>Potassium: </b> </div></td><td><div align="left"> <input type="text" name="potassium" size=25> </div></td></tr> <tr><td><div align="left"><b>Notes: </b> </div></td><td><div align="left"> <p><textarea name="notes" cols="50" rows="10"></textarea></p> </div></td></tr> <tr><th colspan=2><p><input type="submit" value="Add New Test"></p></th></tr> </table> </form> </td> </tr> <tr> <td align="center" valign="top" bgcolor="#FFFFFF"><div align="center"><font size=2> © 2014 </font> </div></td> </tr> </table> </td> </tr> </table> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/290487-drop-down-populates-after-post/ Share on other sites More sharing options...
akphidelt2007 Posted August 16, 2014 Share Posted August 16, 2014 (edited) You create the $userid only when the request method is POST. So when you see the form the $userid is empty and you aren't getting any results until you post the form. Seems like you might not have error checking set to show when a variable has not been set. Edited August 17, 2014 by akphidelt2007 Quote Link to comment https://forums.phpfreaks.com/topic/290487-drop-down-populates-after-post/#findComment-1487981 Share on other sites More sharing options...
ginerjm Posted August 17, 2014 Share Posted August 17, 2014 You really should learn how to plan your coding to keep the logic (the php stuff) separated from the presentation (the css/html stuff). Your script is too hard to decipher the way it is assembled now. I use functions to display my output. Usually just one but sometimes more than that. In that function I have php vars that display the dynamic data of that page (built in the php section and passed into this display function), but all the static html and css code is there, or is included there. No need for any of this to suddenly appear in the middle of your php code. Quote Link to comment https://forums.phpfreaks.com/topic/290487-drop-down-populates-after-post/#findComment-1487985 Share on other sites More sharing options...
mac_gyver Posted August 17, 2014 Share Posted August 17, 2014 some more advantages of rearranging the code as ginerjm has suggested are - 1) it will be easier to update the existing mysql code to use the mysqli or POD database functions, since the mysql functions are obsolete. all the database code will be close together, near the start of the code file. 2) since the main logic that determines what to do on the page and what data is being produced will be grouped closer together, it will be easier to avoid problems like the $userid variable problem. 3) it will make testing and debugging the code easier, since once you have determined that the main php code is producing the correct result, you can forget about that part of the code and concentrate on getting the correct output on the web page. 4) you will be able to perform a header() redirect back to the same page once you have successfully processed the form data, thereby making the user experience better since the browser won't attempt to resubmit the form data should you refresh the page or navigate back to any page that was the target of a form submission. Quote Link to comment https://forums.phpfreaks.com/topic/290487-drop-down-populates-after-post/#findComment-1488042 Share on other sites More sharing options...
msebar Posted August 17, 2014 Author Share Posted August 17, 2014 You create the $userid only when the request method is POST. So when you see the form the $userid is empty and you aren't getting any results until you post the form. Seems like you might not have error checking set to show when a variable has not been set. Yes that is what I was missing. I got it to work but now the selection doesn't post to the database. Quote Link to comment https://forums.phpfreaks.com/topic/290487-drop-down-populates-after-post/#findComment-1488048 Share on other sites More sharing options...
msebar Posted August 17, 2014 Author Share Posted August 17, 2014 You really should learn how to plan your coding to keep the logic (the php stuff) separated from the presentation (the css/html stuff). Your script is too hard to decipher the way it is assembled now. I use functions to display my output. Usually just one but sometimes more than that. In that function I have php vars that display the dynamic data of that page (built in the php section and passed into this display function), but all the static html and css code is there, or is included there. No need for any of this to suddenly appear in the middle of your php code. I know it's a mess, I am in the process of learning and I will work on cleaning it up. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/290487-drop-down-populates-after-post/#findComment-1488049 Share on other sites More sharing options...
mac_gyver Posted August 17, 2014 Share Posted August 17, 2014 the most immediate problem is that your <select> tag doesn't have a name='tank' attribute, so there's no value being submitted to the php code. as akphidelt2007 mentioned, you need to have php's error_reporting set to E_ALL and display_errors set to ON (in your php.ini on your development system) so that php will help you by reporting and displaying all the errors it detects. this will save you a ton of time during the learning and debugging process. Quote Link to comment https://forums.phpfreaks.com/topic/290487-drop-down-populates-after-post/#findComment-1488050 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.