SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Guru ripcurlksm's Avatar
    Join Date
    Aug 2004
    Location
    San Clemente, CA
    Posts
    859
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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 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?

  2. #2
    SitePoint Wizard wonshikee's Avatar
    Join Date
    Jan 2007
    Posts
    1,223
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    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[]'

  3. #3
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,500
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    Code:
    name="geography[]"
    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.

  4. #4
    SitePoint Guru ripcurlksm's Avatar
    Join Date
    Aug 2004
    Location
    San Clemente, CA
    Posts
    859
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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 Code:
    <?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 
    }
    ?>

  5. #5
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,500
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by ripcurlksm View Post
    print_r($POST) works great, but I would like a way to format it to make it look more readable.
    Of course. I didn't know the level of your knowledge, so I said that so you could see the content of the array

    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.

  6. #6
    I solve practical problems. bronze trophy
    Michael Morris's Avatar
    Join Date
    Jan 2008
    Location
    Knoxville TN
    Posts
    2,033
    Mentioned
    65 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ripcurlksm View Post
    but I would like a way to format it to make it look more readable.
    <pre> tags can be used for a block of text that preserves tabs and returns within HTML.

    PHP Code:
    <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.

    PHP Code:
    <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.

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

  7. #7
    SitePoint Guru ripcurlksm's Avatar
    Join Date
    Aug 2004
    Location
    San Clemente, CA
    Posts
    859
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank again!

    here it is for future reference

    PHP Code:
    <?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 
    }
    ?>

  8. #8
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,080
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    To make it fully generic supporting an infinite depth of nested arrays:

    PHP Code:
    <?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
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •