Click to See Complete Forum and Search --> : Sending data from forms to a script as an array?


toicontien
11-18-2003, 11:10 PM
I've just delved into PHP as part of a college class. I'm creating an e-commerce site (for the most part, and only in one week too :mad: ). The form has checkboxes so customers can add multiple items at once. The theory is to name the checkboxes the same so that the values for the checked checkboxes get sent to the script via an array. Example code is below:


<input type="checkbox" name="addItem[]" value="10000" />
.
.
.

<input type="checkbox" name="addItem[]" value="20000" />


The value of each checkbox is the item number and is unique to each product.

Does adding the [] to the end of an input's name cause the values for the input to be sent to a script via an array (or interpreted as an array)?

My grad. student teacher says it's possible and claims to have gotten it working, but I'll be a dead Kennedy if I can figure it out!

The values are sent via the POST method. In the script:


if (isset($_POST["addItem[]"])) {
$add = $_POST["addItem[]"];
}
else {
$add[0] = "";
}


The script always thinks $_POST["addItem[]"] is not set.

I've changed things to the GET method (and the subsequent PHP code to grab the data for the script) and can see the data appended to the URL, but still $add is seen as not set.

I've also placed an integer index (via the script) into name="addItem[]" so that it is addItem[0] ... addItem[n] for each product. Still no luck.

I've got a ton of functions written and I'll gladly post them if needed, but I was just wondering if you can pass multiple values from a form under one name and have the script interpret it as an array.

pyro
11-19-2003, 07:59 AM
Originally posted by toicontien
Does adding the [] to the end of an input's name cause the values for the input to be sent to a script via an array (or interpreted as an array)?Yes, though it sucks that you have to do it like that, as [ and ] are not valid characters in the name attribute. Ah well...

Originally posted by toicontien
I've also placed an integer index (via the script) into name="addItem[]" so that it is addItem[0] ... addItem[n] for each product. Still no luck.You were close. Since it returns it as an array, if you need to access it via the $_POST array, you are going to use it like a multi-dementional array:

$_POST['addItem'][0] to get first item checked...

Anyway, take a look at this following code:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Example</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<form action="" method="post">
<p><input type="checkbox" name="check[]" value="0"> Zero<br>
<input type="checkbox" name="check[]" value="1"> One<br>
<input type="checkbox" name="check[]" value="2"> Two<br>
<input type="submit" name="submit" value="submit"></p>
</form>
<?PHP
if (isset($_POST['submit'])) {
echo "<p>";
foreach ($_POST['check'] as $value) {
echo $value."<br>";
}
# OR
# $_POST['check'][0] -> first item in array
# $_POST['check'][1] -> second item in array
# etc...
echo "</p>";
}
?>
</body>
</html>

toicontien
11-19-2003, 09:52 AM
Thanks, I'll give it a shot.

pyro
11-19-2003, 10:09 AM
You bet... :)