Echo all form data submitted-- checkbox/array issue

I wanted to create a little script to save time on all the “feedback” forms I put on websites. Something where you can point any form to it, and it will print the data on screen (and eventually email).

This is working great, except for checkboxes. its only showing the last checkbox as the value. I’ve tried inserting an is_array() into the loop to specially process checkboxes, etc. but I can’t get it to work. How do I process the checkboxes so it shows all selections on screen?

Working example:
http://ksm.fm/misc/print-form-variables.php

Development code:

<?php

if(array_key_exists("Submit", $_POST)){

    // loop through post data for form
	foreach($_POST as $key => $value){

		echo $key." = ".$value."<br/>";
	
	}      	

	 echo "<i>Submitted!</i>";

} else {
	
?>
<form action="" method="post" name="myform" id="myform">
	
		<div style="float: left;">
		
			<h3>Your Info</h3>
			
			Name: <br /><input id="name" title="" name="name" style='width: 250px;'/><br clear="all"/>
			Title:<br /><input id="title" name="title"/><br clear="all"/>
			Company:<br /><input id="company" name="company" style='width: 250px;'/><br clear="all"/>
			
		</div>
		
		<br clear="all"/>
	
		<p>Geography<br />
		<input type="checkbox" name="geography" value="global"/><label for="global">Global</label>
		<input type="checkbox" name="geography" value="us"/><label for="us">U.S.</label>
		<input type="checkbox" name="geography" value="emea"/><label for="emea">EMEA</label>
		<input type="checkbox" name="geography" value="bric"/><label for="bric">BRIC</label>
		<input type="checkbox" name="geography" value="apac"/><label for="apac">Asia-Pacific</label>
		<input type="checkbox" name="geography" value="lasa"/><label for="lasa">Latin/South America</label>
		<input type="checkbox" name="geography" value="other"/><label for="other">Other</label>
		
		<br clear="all"/>

		<input name="Submit" value="Submit" type="submit" />
		
	</div>
</form>	

<?php
}
?>

How do I process the checkboxes so it shows all selections on screen?

Checkboxes are unique elements, each name must be unique. Radios on the other hand are grouped according to the name, thus must be same.

If you want all the checkboxes to be grouped together, you can use them as an array

name=‘geography

name="geography[B][][/B]"

Notice the square brackets behind the checkbox name. This way, $_POST[‘geography’] will contain an array. Do a print_r to see exactly what is being sent.

thats right… I’m just used to processing checkboxes as arrays, I did it by habit.

Won, renaming the checkbox names did the trick and will do the trick I believe.

guido, thanks for picking up on misnaming the checkbox without “” to make it an array. print_r($POST) works great, but I would like a way to format it to make it look more readable. I’m trying something like this (not working) where I test to see if there is an array within the array, but no luck yet.

<?php

if(array_key_exists("Submit", $_POST)){

    // loop through post data for form
	foreach($_POST as $key => $value){
		
		if(is_array($value)){
			
			foreach($_POST as $key => $value){
				
				echo $key." = ".$value."<br/>";
			}
			
		} else {
			
			echo $key." = ".$value."<br/>";
		}
	}      	

	 echo "<i>Submitted!</i>";

} else {
	
?>
<form action="" method="post" name="myform" id="myform">
	
		<div style="float: left;">
		
			<h3>Your Info</h3>
			
			Name: <br /><input id="name" title="" name="name" style='width: 250px;'/><br clear="all"/>
			Title:<br /><input id="title" name="title"/><br clear="all"/>
			Company:<br /><input id="company" name="company" style='width: 250px;'/><br clear="all"/>
			
		</div>
		
		<br clear="all"/>
	
		<p>Geography<br />
		<input type="checkbox" name="geography[]" value="global"/><label for="global">Global</label>
		<input type="checkbox" name="geography[]" value="us"/><label for="us">U.S.</label>
		<input type="checkbox" name="geography[]" value="emea"/><label for="emea">EMEA</label>
		<input type="checkbox" name="geography[]" value="bric"/><label for="bric">BRIC</label>
		<input type="checkbox" name="geography[]" value="apac"/><label for="apac">Asia-Pacific</label>
		<input type="checkbox" name="geography[]" value="lasa"/><label for="lasa">Latin/South America</label>
		<input type="checkbox" name="geography[]" value="other"/><label for="other">Other</label>
		
		<br clear="all"/>

		<input name="Submit" value="Submit" type="submit" />
		
	</div>
</form>	

<?php
}
?>

Of course. I didn’t know the level of your knowledge, so I said that so you could see the content of the array :slight_smile:

I’m trying something like this (not working) where I test to see if there is an array within the array, but no luck yet.

Inside that if, the foreach mustn’t take $_POST as array, but $value. And rename the $key => $value pair in that second foreach to avoid interferences with the first foreach loop.

<pre> tags can be used for a block of text that preserves tabs and returns within HTML.


<pre><?php print_r($_POST); ?></pre>

But even better, if you have firebug on firefox, is to send the debug stuff to the console where it doesn’t interrupt the layout.


<script type="text/javascript">
  console.dir('<?php echo json_encode( $_POST ) ?>');
</script>

If you use the prototype.js framework you may wish to cast the $_POST as an object to keep from having to read the prototype extensions to the Array object in the console.dir call.


<script type="text/javascript">
  console.dir('<?php echo json_encode( (object) $_POST ) ?>');
</script>

Thank again!

here it is for future reference

<?php

if(array_key_exists("Submit", $_POST)){

    // loop through post data for form
	foreach($_POST as $key => $value){
		
		if(is_array($value)){
			
			foreach($value as $key2 => $value2){
				
				echo $key." = ".$value2."<br/>";
			}
			
		} else {
			
			echo $key." = ".$value."<br/>";
		}
	}      	

	 echo "<i>Submitted!</i>";

} else {
	
?>
<form action="" method="post" name="myform" id="myform">
	
		<div style="float: left;">
		
			<h3>Your Info</h3>
			
			Name: <br /><input id="name" title="" name="name" style='width: 250px;'/><br clear="all"/>
			Title:<br /><input id="title" name="title"/><br clear="all"/>
			Company:<br /><input id="company" name="company" style='width: 250px;'/><br clear="all"/>
			
		</div>
		
		<br clear="all"/>
	
		<p>Geography<br />
		<input type="checkbox" name="geography[]" value="global"/><label for="global">Global</label>
		<input type="checkbox" name="geography[]" value="us"/><label for="us">U.S.</label>
		<input type="checkbox" name="geography[]" value="emea"/><label for="emea">EMEA</label>
		<input type="checkbox" name="geography[]" value="bric"/><label for="bric">BRIC</label>
		<input type="checkbox" name="geography[]" value="apac"/><label for="apac">Asia-Pacific</label>
		<input type="checkbox" name="geography[]" value="lasa"/><label for="lasa">Latin/South America</label>
		<input type="checkbox" name="geography[]" value="other"/><label for="other">Other</label>
		
		<br clear="all"/>

		<input name="Submit" value="Submit" type="submit" />
		
	</div>
</form>	

<?php 
}
?>

To make it fully generic supporting an infinite depth of nested arrays:

<?php
function printData($data)
{
   foreach($data as $k=>$v)
   {
      if (is_array($v))
         printData($v);
      else
         echo $k.'='.$v;
   }
}
if(array_key_exists("Submit", $_POST)){

   printData($_POST);      	

   echo "<i>Submitted!</i>";

} else {
	
?>
<form action="" method="post" name="myform" id="myform">
	
		<div style="float: left;">
		
			<h3>Your Info</h3>
			
			Name: <br /><input id="name" title="" name="name" style='width: 250px;'/><br clear="all"/>
			Title:<br /><input id="title" name="title"/><br clear="all"/>
			Company:<br /><input id="company" name="company" style='width: 250px;'/><br clear="all"/>
			
		</div>
		
		<br clear="all"/>
	
		<p>Geography<br />
		<input type="checkbox" name="geography[]" value="global"/><label for="global">Global</label>
		<input type="checkbox" name="geography[]" value="us"/><label for="us">U.S.</label>
		<input type="checkbox" name="geography[]" value="emea"/><label for="emea">EMEA</label>
		<input type="checkbox" name="geography[]" value="bric"/><label for="bric">BRIC</label>
		<input type="checkbox" name="geography[]" value="apac"/><label for="apac">Asia-Pacific</label>
		<input type="checkbox" name="geography[]" value="lasa"/><label for="lasa">Latin/South America</label>
		<input type="checkbox" name="geography[]" value="other"/><label for="other">Other</label>
		
		<br clear="all"/>

		<input name="Submit" value="Submit" type="submit" />
		
	</div>
</form>	

<?php
}
?>

Just in case you ever need it :slight_smile: