...

View Full Version : Posting Array



latemodern
06-02-2007, 08:40 PM
Hi,

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



echo "
<ul class='checkboxes'>";

$category_query = "select * from alumni_category";
$result = @mysql_query($category_query);
while ($row = mysql_fetch_array($result, MYSQL_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:



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

CFMaBiSmAd
06-02-2007, 08:49 PM
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.

latemodern
06-02-2007, 08:56 PM
Really? I can't see anything unusual. Can you?


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

mr e
06-02-2007, 09:19 PM
Try putting this at the top to see if the $_POST is being populated properly


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

latemodern
06-02-2007, 09:41 PM
This is what it says:

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

Thanks

mr e
06-02-2007, 09:50 PM
Well, I just tried this simple example and it worked like it should


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


Array
(
[category] => Array
(
[0] => 00001
[1] => 00006
[2] => 00007
[3] => 00008
[4] => 00009
)

[submit] => submit
)

latemodern
06-02-2007, 10:07 PM
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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum