Jump to content

Dynamic Forms


Hooch
 Share

Recommended Posts

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

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

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.

Edited by Hooch
Link to comment
Share on other sites

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.

Edited by Hooch
Link to comment
Share on other sites

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...