Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 06-02-2007, 07:40 PM   PM User | #1
latemodern
New Coder

 
Join Date: Mar 2006
Posts: 66
Thanks: 3
Thanked 0 Times in 0 Posts
latemodern is an unknown quantity at this point
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
__________________
Chris Holbrook
Freelance Designer and Musician
Freelance Web Designer and Musician: Bristol, UK
Visit my site: http://www.chrisholbrook.com

Last edited by latemodern; 06-02-2007 at 07:43 PM..
latemodern is offline   Reply With Quote
Old 06-02-2007, 07:49 PM   PM User | #2
CFMaBiSmAd
Senior Coder

 
CFMaBiSmAd's Avatar
 
Join Date: Oct 2006
Location: Denver, Colorado USA
Posts: 2,712
Thanks: 2
Thanked 251 Times in 243 Posts
CFMaBiSmAd is a jewel in the roughCFMaBiSmAd is a jewel in the roughCFMaBiSmAd is a jewel in the roughCFMaBiSmAd is a jewel in the rough
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.
CFMaBiSmAd is offline   Reply With Quote
Old 06-02-2007, 07:56 PM   PM User | #3
latemodern
New Coder

 
Join Date: Mar 2006
Posts: 66
Thanks: 3
Thanked 0 Times in 0 Posts
latemodern is an unknown quantity at this point
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
latemodern is offline   Reply With Quote
Old 06-02-2007, 08:19 PM   PM User | #4
mr e
Regular Coder

 
Join Date: Apr 2007
Posts: 295
Thanks: 0
Thanked 19 Times in 19 Posts
mr e is on a distinguished road
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..
mr e is offline   Reply With Quote
Old 06-02-2007, 08:41 PM   PM User | #5
latemodern
New Coder

 
Join Date: Mar 2006
Posts: 66
Thanks: 3
Thanked 0 Times in 0 Posts
latemodern is an unknown quantity at this point
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
latemodern is offline   Reply With Quote
Old 06-02-2007, 08:50 PM   PM User | #6
mr e
Regular Coder

 
Join Date: Apr 2007
Posts: 295
Thanks: 0
Thanked 19 Times in 19 Posts
mr e is on a distinguished road
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
)
mr e is offline   Reply With Quote
Old 06-02-2007, 09:07 PM   PM User | #7
latemodern
New Coder

 
Join Date: Mar 2006
Posts: 66
Thanks: 3
Thanked 0 Times in 0 Posts
latemodern is an unknown quantity at this point
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
latemodern is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:30 AM.


Advertisement
Log in to turn off these ads.