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