I want to create a multidimensional array for some textboxes, all with the same name
| Code: |
//This sits in a loop so there are many textboxes named qty[]
Qty:<input type='text' size='2' name='qty[]' value='1'>
//on the process page
$id=$_GET['id'];
$qty=$_POST['qty'][$id];
|
I've been searching for ages trying to find out what's wrong, can anyone point me in the right direction?
| woodenbrick wrote: |
I want to create a multidimensional array for some textboxes, all with the same name
| Code: |
//This sits in a loop so there are many textboxes named qty[]
Qty:<input type='text' size='2' name='qty[]' value='1'>
//on the process page
$id=$_GET['id'];
$qty=$_POST['qty'][$id];
|
I've been searching for ages trying to find out what's wrong, can anyone point me in the right direction? |
There's nothing wrong with the code in itself I think. What are you trying to accomplish?
Well I want to put $qty into a database to track how many of each item someone has bought, but it always comes out as zero no matter what quantity I enter.
I'm confused. Why should it be an array and not just an integer then?
I don't see anything wrong with that code. But may be $id is having a wrong value. Just print the value of $id and see if it is empty or the index is out of range.
You may also want to convert $id into integer before using it ( $id+0 will do), since the array values are indexed against integer, not string.
Eg:
| Code: |
<input type="text" name="test[]" value="A">
<input type="text" name="test[]" value="B">
<input type="text" name="test[]" value="C">
<input type="text" name="test[]" value="D">
$test = $_POST['test'];
// In PHP, the test is an array like
array(
0 => "A",
1 => "B",
2 => "C",
3 => "D"
)
|
For those who are confused about exactly what I am doing here is a link:
shopping cart
Also I put up the source code as well.
kv, I've tried this with $id, it's definitely an integer value and not empty.
When clicking the link, you're not actually submitting the form, you're just loading page process.php?id=1.
You should change the Add to cart button to a submit button.
Ok thanks Stubru I am a bit of a tard but have it sort of working now, will finish it off when I have time.
Thanks for your help!