Hooch Posted October 23, 2009 Share Posted October 23, 2009 Hey all.I have some code that pulls data from a table for a form.The form displays fine but the printing of results is not as it should be.If all entries are checked the posted results only shows about half. <?PHP session_start(); include 'db.php'; if(!$_POST['add']) { echo '<form action="test.php" method="POST">'; $sql = "SELECT * FROM `features` ORDER BY `feature` ASC"; $result = mysql_query($sql); while($r = mysql_fetch_array($result)) { echo $r['feature'] . '<input type="checkbox" name="' . $r['feature'] . '" value="' . $r['feature'] . '"> <br> '; } echo '<input type="submit" value="Submit" name="add">'; echo '</form>'; } else { $sql2 = "SELECT * FROM `features` ORDER BY `feature` ASC"; $result2 = mysql_query($sql2); while($r2 = mysql_fetch_array($result2)) { $var = $r2['feature']; if(isset($_POST[$var])) echo $_POST[$var] . '<br>'; } }?> Any help would be great.Thank you Link to comment Share on other sites More sharing options...
justsomeguy Posted October 23, 2009 Share Posted October 23, 2009 Make sure the names are valid, you may need to escape some characters or something. If in doubt, look at the HTML that it generates for the form for the different checkboxes. Link to comment Share on other sites More sharing options...
Hooch Posted October 24, 2009 Author Share Posted October 24, 2009 Here is the form before posting (view source) <form action="test.php" method="POST">Features: 001-Test Feature<input type="checkbox" name="001-Test Feature" value="001-Test Feature"> <br> Air Conditioning<input type="checkbox" name="Air Conditioning" value="Air Conditioning"> <br> Barn<input type="checkbox" name="Barn" value="Barn"> <br> Controlled Access<input type="checkbox" name="Controlled Access" value="Controlled Access"> <br> Deck<input type="checkbox" name="Deck" value="Deck"> <br> Dishwasher<input type="checkbox" name="Dishwasher" value="Dishwasher"> <br> Electric Heating<input type="checkbox" name="Electric Heating" value="Electric Heating"> <br> Fireplace<input type="checkbox" name="Fireplace" value="Fireplace"> <br> Garbage Disposal<input type="checkbox" name="Garbage Disposal" value="Garbage Disposal"> <br> Gas Heating<input type="checkbox" name="Gas Heating" value="Gas Heating"> <br> Hardwood Floors<input type="checkbox" name="Hardwood Floors" value="Hardwood Floors"> <br> Horse Facilities<input type="checkbox" name="Horse Facilities" value="Horse Facilities"> <br> Irrigated Land<input type="checkbox" name="Irrigated Land" value="Irrigated Land"> <br> Jacuzzi/Spa<input type="checkbox" name="Jacuzzi/Spa" value="Jacuzzi/Spa"> <br> Pool<input type="checkbox" name="Pool" value="Pool"> <br> Propane Heating<input type="checkbox" name="Propane Heating" value="Propane Heating"> <br> Security<input type="checkbox" name="Security" value="Security"> <br> Skylights<input type="checkbox" name="Skylights" value="Skylights"> <br> Spinkler System<input type="checkbox" name="Spinkler System" value="Spinkler System"> <br> Vaulted Ceilings<input type="checkbox" name="Vaulted Ceilings" value="Vaulted Ceilings"> <br> View Property<input type="checkbox" name="View Property" value="View Property"> <br> Walk-in Closets<input type="checkbox" name="Walk-in Closets" value="Walk-in Closets"> <br> Waterfront<input type="checkbox" name="Waterfront" value="Waterfront"> <br> Well/Septic<input type="checkbox" name="Well/Septic" value="Well/Septic"> <br> Woodstove<input type="checkbox" name="Woodstove" value="Woodstove"> <br> <input type="submit" value="Submit" name="add"> </form> it is as it should be.Now if I check every box, then submit... (view source) Barn<br>Deck<br>Dishwasher<br>Fireplace<br>Jacuzzi/Spa<br>Pool<br>Security<br>Skylights<br>Waterfront<br>Well/Septic<br>Woodstove<br> ?? Link to comment Share on other sites More sharing options...
jlhaslip Posted October 24, 2009 Share Posted October 24, 2009 Where is the code that checks the form results? Link to comment Share on other sites More sharing options...
Hooch Posted October 24, 2009 Author Share Posted October 24, 2009 To make things a lot easier I condensed the code to what you see in post 1.The results are from that very code. Link to comment Share on other sites More sharing options...
jlhaslip Posted October 24, 2009 Share Posted October 24, 2009 To make things a lot easier I condensed the code to what you see in post 1.The results are from that very code.post the code that checks the form submission and performs the echos. Link to comment Share on other sites More sharing options...
Hooch Posted October 24, 2009 Author Share Posted October 24, 2009 It's in the 1st post... $sql2 = "SELECT * FROM `features` ORDER BY `feature` ASC"; $result2 = mysql_query($sql2); while($r2 = mysql_fetch_array($result2)) { $var = $r2['feature']; if(isset($_POST[$var])) echo $_POST[$var] . '<br>'; } If the box is checked ( if(isset($_POST[$var])) ) it echo's the result. Link to comment Share on other sites More sharing options...
jlhaslip Posted October 25, 2009 Share Posted October 25, 2009 try the same query using phpmyadminnotice that many of the ones missing have a space in the feature name? Link to comment Share on other sites More sharing options...
Hooch Posted October 25, 2009 Author Share Posted October 25, 2009 Thanks for the replies jlhaslip.You seem to be correct on the spaces.I queried in phpmyadmin and it worked 100% Link to comment Share on other sites More sharing options...
Hooch Posted October 25, 2009 Author Share Posted October 25, 2009 It's weird how both mysql queries are identical, but the 1 doesn't echo entries with spaces. Link to comment Share on other sites More sharing options...
jlhaslip Posted October 25, 2009 Share Posted October 25, 2009 is this a single entry table?can you use an array instead of a mysql query? Link to comment Share on other sites More sharing options...
Hooch Posted October 25, 2009 Author Share Posted October 25, 2009 no there will be many entries. Link to comment Share on other sites More sharing options...
Hooch Posted October 26, 2009 Author Share Posted October 26, 2009 I went ahead and replaced the spaces with an underline ( _ ).It makes the query work correctly now. I guess I can add / remove the underline as need be for the site, but any reasons for this problem?Thanks for your time. Link to comment Share on other sites More sharing options...
justsomeguy Posted October 26, 2009 Share Posted October 26, 2009 The spaces in the names automatically get replaced with underscores. Print out $_POST to see what happens when the names have spaces:print_r($_POST);That should print something like this: Array( [001-Test_Feature] => 001-Test Feature [Air_Conditioning] => Air Conditioning [Barn] => Barn [Controlled_Access] => Controlled Access [Deck] => Deck [Dishwasher] => Dishwasher [Electric_Heating] => Electric Heating [Fireplace] => Fireplace [Garbage_Disposal] => Garbage Disposal [Gas_Heating] => Gas Heating [Hardwood_Floors] => Hardwood Floors [Horse_Facilities] => Horse Facilities [Irrigated_Land] => Irrigated Land [Jacuzzi/Spa] => Jacuzzi/Spa [Pool] => Pool [Propane_Heating] => Propane Heating [Security] => Security [Skylights] => Skylights [Spinkler_System] => Spinkler System [Vaulted_Ceilings] => Vaulted Ceilings [View_Property] => View Property [Walk-in_Closets] => Walk-in Closets [Waterfront] => Waterfront [Well/Septic] => Well/Septic [Woodstove] => Woodstove [add] => Submit) Link to comment Share on other sites More sharing options...
Hooch Posted October 30, 2009 Author Share Posted October 30, 2009 Hi Guy!You are correct it displays as you posted.So why does this not show results with spaces? Link to comment Share on other sites More sharing options...
justsomeguy Posted November 1, 2009 Share Posted November 1, 2009 I'm not sure why it converts spaces to underscores. That's probably something left over from years ago. Link to comment Share on other sites More sharing options...
Hooch Posted November 1, 2009 Author Share Posted November 1, 2009 Well thanks for the help guys.I fixed it by adding the underscore upon Db entry via the form. Then I remove it before it gets printed out.It works, but with some tweaks.Again thank you very much. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.