PDA

View Full Version : Multidimensional array problem!


KeithMcL
Wed 22nd Jun '05, 7:49pm
I have a form I'm working on where I'm able to add an item to several categories. The form is like so:

<form action="sale.php?action=add&confirmed=no" method="post" name="addsale">
<table border="0" cellspacing="0" cellpadding="4">
<tr>
<td align="right">Name:</td>
<td><input type="text" name="name" size="34" maxlength="100" /></td>
</tr>
<tr>

<td align="right" valign="top">Description:</td>
<td><textarea rows="6" cols="22" name="description"></textarea></td>
</tr>
<tr>
<td valign="top" align="right">Category:</td>
<td>
<select name="categories[]" multiple="multiple">
<optgroup label="Clothing & Accessories">
<option label="mens-clothing" value="4">Mens Clothing</option>
<option label="womens-clothing" value="5">Womens Clothing</option>
<option label="kids-baby-clothing" value="6">Kids & Baby Clothing</option>
<option label="bags-backpacks" value="15">Bags & Backpacks</option>

<option label="hats-caps" value="16">Hats & Caps</option>
<option label="jewellery" value="17">Jewellery</option>
</optgroup>
</select>
</td>
</tr>
<tr>
<td align="right">Discount:</td>
<td><input type="text" name="discount" size="1" maxlength="3" />%</td>
</tr>

<tr>
<td>&nbsp;</td>
<td><input type="submit" name="submit" value="Add Sale" /></td>
</tr>
</table>
</form>Now, the categories select should return an array inside the $_POST array.

The problem is, when the form is submitted the categories element is being recognised as a string with the value "Array".

Anyone got any ideas why this is happening?

A var_dump of $_POST comes out like this:
array(5) { ["name"]=> string(13) "June Bargains" ["description"]=> string(13) "June Bargains" ["categories"]=> string(5) "Array" ["discount"]=> string(2) "10" ["submit"]=> string(8) "Add Sale" }This only happens on my host (using php 4), it works fine locally (using php5).

KeithMcL
Wed 22nd Jun '05, 7:50pm
Btw, there's a little bit more info on this here:
http://forum3.midphasetalk.com/showthread.php?t=3910

merk
Wed 22nd Jun '05, 8:53pm
Try removing the [] from the name of the select.

KeithMcL
Wed 22nd Jun '05, 10:40pm
Try removing the [] from the name of the select.
That didn't work. It's still showing "Array" as the value. I think I'll try removing some of the other fields on the form to see if that makes any difference.

Lats
Thu 23rd Jun '05, 8:25pm
You'll need to extract the categories array separately, something like this...
$cats = $_POST['categories'];
if(is_array($cats))
{
echo "something selected";
}
else
{
echo "nothing selected";
}

KeithMcL
Fri 24th Jun '05, 4:01pm
I already have it like that and it still doesn't work. It's being converted to a string during the submission so nothing I do with the $_POST array helps.

RagingPenguin
Sat 25th Jun '05, 2:52am
I just tested this on my box and it works fine. Try it on yours and see what happens.

http://ragingpenguin.com/select.php


<?php
$categories = $_POST['categories'];
if (!is_array($categories)){ $categories = array(); }
$count = count($categories);
$selected = implode(', ', $categories);
echo <<<OUT
<html>
<body>
<form method="post" action="select.php">
Previous count: $count<br>
Previous selected: $selected<br><br>
<select name="categories[]" multiple="multiple">
<optgroup label="Clothing & Accessories">
<option label="mens-clothing" value="4">Mens Clothing</option>
<option label="womens-clothing" value="5">Womens Clothing</option>
<option label="kids-baby-clothing" value="6">Kids & Baby Clothing</option>
<option label="bags-backpacks" value="15">Bags & Backpacks</option>
<option label="hats-caps" value="16">Hats & Caps</option>
<option label="jewellery" value="17">Jewellery</option>
</optgroup>
</select><br>
<input type="submit" value="Submit">
</form>
</body>
</html>
OUT;
?>

KeithMcL
Sat 25th Jun '05, 10:12am
That seems to work fine for me too.

http://whatsales.com/select.php

I think I'm over complicating my form submission process. I'm using the switch function so I can have a multi-page form. I guess I need to simplify the whole process.

Thanks everyone for your help :)

RagingPenguin
Sat 25th Jun '05, 11:16am
Try removing the ?a=b params from your form submit url and make them hidden input fields. I seem to remember having problems with php4 when mixing the two methods. Try that and see if your original script then works.

KeithMcL
Sat 25th Jun '05, 12:16pm
Good idea. I'll try that and see how I get on. Thanks again.

KeithMcL
Tue 28th Jun '05, 8:44pm
It seems the dreaded register_globals setting is what's been causing me all sorts of grief!

http://bugs.php.net/bug.php?id=23454

I've asked my host if I (or them) can add a line to a .htaccess to turn them off.