Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7

Thread: Posting Array

  1. #1
    New Coder
    Join Date
    Mar 2006
    Posts
    66
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Posting Array

    Hi,

    I am setting up a page on which users select a number of checkboxes:

    PHP Code:
    echo "
    <ul class='checkboxes'>"
    ;    

    $category_query    "select * from alumni_category";
    $result = @mysql_query($category_query);
    while (
    $row mysql_fetch_array($resultMYSQL_ASSOC))
    {
    echo 
    '
    <li class="inline">
    <input type="checkbox" id = "' 
    $row[categoryid] . '" name="'category[]" value="' . $row[categoryid] . '" /><label for= "' . $row[categoryid] . '">' . $row[category] . "</label></li>n";
    }
    echo '
    </ul>';

    The code to handle this is:

    PHP Code:
    if(isset($_POST['category']) && count($_POST['category']) !=0){
         
    $category = array();
         foreach(
    $_POST['category'] as $val){
             
    $category[] = $val;                
         }
    } else {
         
    $category FALSE;
         
    $errors[] = 'Please choose at least one category.';

    The var_dump prints out the following, no matter how many checkboxes I check:

    string(5) "Array" Warning: Invalid argument supplied for foreach() in mywebsite.com/test/cv.php on line 107 array(0) { }
    echo var_dump($category);

    Why is it posting a string saying 'Array' rather than an array of the values that I chose?

    Any help would be greatly appreciated

    Thanks
    Last edited by latemodern; 06-02-2007 at 07:43 PM.
    Chris Holbrook
    Freelance Designer and Musician
    Freelance Web Designer and Musician: Bristol, UK
    Visit my site: http://www.chrisholbrook.com

  • #2
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,066
    Thanks
    2
    Thanked 319 Times in 311 Posts
    Use your browser's "view source" to look at the form code that is getting created and/or take a look at the code highlighting in your post. There are some incorrect quotes that is causing the name="category[]" to be malformed.
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #3
    New Coder
    Join Date
    Mar 2006
    Posts
    66
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Really? I can't see anything unusual. Can you?

    Code:
    <ul class='checkboxes'><li class="inline">
    <input type="checkbox" id = "00001" name="category[]" value="00001" /><label for= "00001">sculpture</label></li>
    <li class="inline"><input type="checkbox" id = "00002" name="category[]" value="00002" /><label for= "00002">galleries</label></li>
    <li class="inline"><input type="checkbox" id = "00003" name="category[]" value="00003" /><label for= "00003">Sculpure</label></li>
    <li class="inline"><input type="checkbox" id = "00004" name="category[]" value="00004" /><label for= "00004">galleries</label></li>
    <li class="inline"><input type="checkbox" id = "00005" name="category[]" value="00005" /><label for= "00005">galleries</label></li>
    <li class="inline"><input type="checkbox" id = "00006" name="category[]" value="00006" /><label for= "00006">Sculpture</label></li>
    <li class="inline"><input type="checkbox" id = "00007" name="category[]" value="00007" /><label for= "00007">Sculpture</label></li>
    <li class="inline"><input type="checkbox" id = "00008" name="category[]" value="00008" /><label for= "00008">galleries</label></li>
    <li class="inline"><input type="checkbox" id = "00009" name="category[]" value="00009" /><label for= "00009">sculpture</label></li>
    <li class="inline"><input type="checkbox" id = "00010" name="category[]" value="00010" /><label for= "00010">galleries</label></li>
    <li class="inline"><input type="checkbox" id = "00011" name="category[]" value="00011" /><label for= "00011">galleries</label></li>
    <li class="inline"><input type="checkbox" id = "00012" name="category[]" value="00012" /><label for= "00012">galleries</label></li>
    <li class="inline"><input type="checkbox" id = "00013" name="category[]" value="00013" /><label for= "00013">galleries</label></li>
    <li class="inline"><input type="checkbox" id = "00014" name="category[]" value="00014" /><label for= "00014">sculpture</label></li>
    <li class="inline"><input type="checkbox" id = "00015" name="category[]" value="00015" /><label for= "00015">sculpture</label></li>
    <li class="inline"><input type="checkbox" id = "00016" name="category[]" value="00016" /><label for= "00016">sculpture</label></li>
    </ul>
    Chris Holbrook
    Freelance Designer and Musician
    Freelance Web Designer and Musician: Bristol, UK
    Visit my site: http://www.chrisholbrook.com

  • #4
    Regular Coder
    Join Date
    Apr 2007
    Posts
    295
    Thanks
    0
    Thanked 19 Times in 19 Posts
    Try putting this at the top to see if the $_POST is being populated properly
    PHP Code:
    print_r($_POST); 
    Instead of isset($_POST['category']), you might try is_array() to make sure it's an array

    Also, the only time count() will return 0 is if the value is NULL, so if it is set, it won't return 0 with count anyway
    Last edited by mr e; 06-02-2007 at 08:24 PM.

  • #5
    New Coder
    Join Date
    Mar 2006
    Posts
    66
    Thanks
    3
    Thanked 0 Times in 0 Posts
    This is what it says:

    Array ( [MAX_FILE_SIZE] => 2097152 [category] => Array [submitted] => TRUE [submit] => Submit )

    Thanks
    Chris Holbrook
    Freelance Designer and Musician
    Freelance Web Designer and Musician: Bristol, UK
    Visit my site: http://www.chrisholbrook.com

  • #6
    Regular Coder
    Join Date
    Apr 2007
    Posts
    295
    Thanks
    0
    Thanked 19 Times in 19 Posts
    Well, I just tried this simple example and it worked like it should
    PHP Code:
    <?php
    if($_POST['submit'])
    {
         echo 
    '<pre>';
         
    print_r($_POST);
         echo 
    '</pre>';
    }
    ?>

    <hr>
    <form method="post" action="">
    <ul class='checkboxes'><li class="inline">
    <input type="checkbox" id = "00001" name="category[]" value="00001" /><label for= "00001">sculpture</label></li>
    <li class="inline"><input type="checkbox" id = "00002" name="category[]" value="00002" /><label for= "00002">galleries</label></li>
    <li class="inline"><input type="checkbox" id = "00003" name="category[]" value="00003" /><label for= "00003">Sculpure</label></li>
    <li class="inline"><input type="checkbox" id = "00004" name="category[]" value="00004" /><label for= "00004">galleries</label></li>
    <li class="inline"><input type="checkbox" id = "00005" name="category[]" value="00005" /><label for= "00005">galleries</label></li>
    <li class="inline"><input type="checkbox" id = "00006" name="category[]" value="00006" /><label for= "00006">Sculpture</label></li>
    <li class="inline"><input type="checkbox" id = "00007" name="category[]" value="00007" /><label for= "00007">Sculpture</label></li>
    <li class="inline"><input type="checkbox" id = "00008" name="category[]" value="00008" /><label for= "00008">galleries</label></li>
    <li class="inline"><input type="checkbox" id = "00009" name="category[]" value="00009" /><label for= "00009">sculpture</label></li>
    <li class="inline"><input type="checkbox" id = "00010" name="category[]" value="00010" /><label for= "00010">galleries</label></li>
    <li class="inline"><input type="checkbox" id = "00011" name="category[]" value="00011" /><label for= "00011">galleries</label></li>
    <li class="inline"><input type="checkbox" id = "00012" name="category[]" value="00012" /><label for= "00012">galleries</label></li>
    <li class="inline"><input type="checkbox" id = "00013" name="category[]" value="00013" /><label for= "00013">galleries</label></li>
    <li class="inline"><input type="checkbox" id = "00014" name="category[]" value="00014" /><label for= "00014">sculpture</label></li>
    <li class="inline"><input type="checkbox" id = "00015" name="category[]" value="00015" /><label for= "00015">sculpture</label></li>
    <li class="inline"><input type="checkbox" id = "00016" name="category[]" value="00016" /><label for= "00016">sculpture</label></li>
    </ul>

    <input type="submit" name="submit" value="submit />
    </form>
    and the output
    Code:
    Array
    (
        [category] => Array
            (
                [0] => 00001
                [1] => 00006
                [2] => 00007
                [3] => 00008
                [4] => 00009
            )
    
        [submit] => submit
    )

  • #7
    New Coder
    Join Date
    Mar 2006
    Posts
    66
    Thanks
    3
    Thanked 0 Times in 0 Posts
    That's really strange. I copied and pasted the above code into a test page, ticked loads of the boxes and it gave me this:

    Array
    (
    [category] => Array
    [submit] => submit
    )

    do you think there might be something up with the server config?

    Thanks
    Chris Holbrook
    Freelance Designer and Musician
    Freelance Web Designer and Musician: Bristol, UK
    Visit my site: http://www.chrisholbrook.com


  •  

    Posting Permissions

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