Page 1 of 1

submit script generated forms

Posted: Mon Jan 15, 2007 7:11 pm
by Think Pink
Hello
Here is my problem, hope someone can help.

I have a table "categories" structured like this.
category_id | title | type | values
21 | Resolution | multiselect | 800x600, 1024x768, 1280x1024
28 | Name | input | null
31 | Colour | input | null
..............

this information is taken from the database and acording to it's "type" filed listed.

for example

Code: Select all

<form name="test" action="add_records.php" method="post">
<table>
<tr>
<td>Resolution</td>
<td><select size="5" multiple name="cat[<?=$row['id'];?>]"><option value="800x600">800x600</option><option value="1024x768">1024x768</option><option value="1280x1024">1280x1024</option></select></td>
</tr>
<tr>
<td>Name</td>
<td><input type="text" name="cat[<?=$row['id'];?>]"></td>
</tr>
<tr>
<td>Colour</td>
<td><input type="text" name="cat[<?=$row['id'];?>]"></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="save" name="submit_form"></td>
</table>
</form>
... so far all is ok and working perfectly.

the problems appear when I try to submit the form, because if I choose 2 or more values from the multiselect form, it takes me only the last record.

for example complete the form like this.
Resolution : 1024x768, 1280x1024 "this is a multiselect so I Ctrl select multiple values"
Name : Jim
Colour: red

When I submit the form I get
Array([21]=>"1280x1024", [28]=>"Jim", [31]=>"red")

And I need
Array([21]=>"1024x768", [21]=>"1280x1024", [28]=>"Jim", [31]=>"red")

This info is added into a table "products" wich has the following structure
id | product_id | category_id | values

and it should be added like this.
1 | 1 | 21 | 1024x768
2 | 1 | 21 | 1280x1024
3 | 1 | 28 | Jim
4 | 1 | 31 | Red

I would be very thankfull if you could help me solve the problem.
thankyou in advance.
Alin

Posted: Mon Jan 15, 2007 8:09 pm
by Ollie Saunders
Array([21]=>"1280x1024", [28]=>"Jim", [31]=>"red")
Show the code that produces that data. I'm sure you didn't print_r($_POST) to get that.

submit script generated forms

Posted: Tue Jan 16, 2007 3:38 am
by Think Pink
Show the code that produces that data. I'm sure you didn't print_r($_POST) to get that.
Yes you are right, I just wrioe it to have an ideea.

this is the print_r
Array
(
[15] => Array([0] => Jim)
[17] => Array([0] => ) // the form wasn't filled here so this is empty
[18] => Array([0] => some text)
[19] => Array([0] => ) // the form wasn't filled here so this is empty
[21] => Array([0] => 800x600 [1] => 1024x768)
) save

!!! notice the save

this is the code that prints the array

Code: Select all

foreach($_POST as $keys => $values) {
	print_r($values);
}
Problems
1. the save button shouldn't be there
2. If I try to do another foreach inside the first to retrieve all the data to insert into the db I receive an error :
Warning: Invalid argument supplied for foreach() in C:\Apache2\htdocs\test\edit.php on line 12

things should be like :

Code: Select all

insert into products (product_id, category_id, values) values ('".$product_id."', '15', 'Jim')
insert into products (product_id, category_id, values) values ('".$product_id."', '18', 'some text')
insert into products (product_id, category_id, values) values ('".$product_id."', '21', '800x600')
insert into products (product_id, category_id, values) values ('".$product_id."', '21', '1024x768')
Hope this is a better explanation of what I mean.
Thx for your time

Posted: Tue Jan 16, 2007 5:17 am
by Ollie Saunders
The save button should be there, its part of your form, you don't have to use it and you can't stop the browser sending it. Why are you using array subscript notation on your name attributes?
<select size="5" multiple name="cat[<?=$row['id'];?>]">
<input type="text" name="cat[<?=$row['id'];?>]">
<input type="text" name="cat[<?=$row['id'];?>]">
If you don't have a good reason for doing that you shouldn't be doing so.

I suggest you use a name of 'resolution' for the select field, and then 'name', and 'colour' for the other two. If you do that you will get an array with those elements (plus save, which you can ignore) where the element 'resolution' is an array of the multiple choices.