Page 1 of 2

lots of check boxes! is there an quick way of entering

Posted: Fri Mar 05, 2004 9:05 am
by nutstretch
I have a product wich can have 1 feature or any amount up to 16 features.

I have a form which has 16 checkboxes on for the results

my code needs to see which checkboxes are checked and place them into a table. IDNo of feature and product id no

At present i envisage having to go through all 16 and check to see if they are checked and inseting using code smoehting like this.
$sql = "INSERT INTO tblfeaturedetails (FeatureID, SytleID) VALUES ('$feature1', '$StyleID')";
$result1 = mysql_query($sql,$linkID)or die(mysql_error());
if ($result1 == true)
{
}
else
{
print "Not able to add feature";
}

this way means that I would have to copy the code for each checkbox.

Is there a way of using the check boxes in an array and using a for loop to add these.

Any help appreciated

Posted: Fri Mar 05, 2004 9:09 am
by JayBird
Just name your check boxes like this

Code: Select all

<input type="checkbox" name="features&#1111;]" value="some_feature">
Then, all the values will be available in an array called fetaures which you can iterate thorugh when the form has been submitted.

Mark

Posted: Fri Mar 05, 2004 9:15 am
by nutstretch
Thanks for your reply

how do you read then on the next form then.

I am using at present

$feature1 = $_POST['feature1'];
$featute2 = $_POST['feature2']; etc.
Can i call an array from my php form which reads them all in?

Posted: Fri Mar 05, 2004 9:19 am
by JayBird
something like

Code: Select all

foreach($_POST['features'] as $feature) {
    // Do your stuff
}
Change $_POST to $_GET depending on your form method

Mark

Posted: Fri Mar 05, 2004 9:24 am
by nutstretch
cheers
so that collects each array part in turn from the previous form
allowing you to 'do your stuff'

Posted: Fri Mar 05, 2004 9:26 am
by JayBird
it surely does :)

Posted: Fri Mar 05, 2004 9:27 am
by nutstretch
Many thanks for you polite (not saying how thick i am) and speedy replies

Angie

Posted: Fri Mar 05, 2004 9:30 am
by nutstretch
do I have to put the [] brackets in the $_POST['feature[]'];

Posted: Fri Mar 05, 2004 9:34 am
by JayBird
nutstretch wrote:do I have to put the [] brackets in the $_POST['feature[]'];
No

Posted: Fri Mar 05, 2004 10:50 am
by nutstretch
I acn't get the $feature to pick up the check boxes

my code is

foreach($_POST['feature'] as $feature)
{
print "$feature";
if (empty($feature) == true)
{
}
else
{
$sql = "INSERT INTO tblfeaturedetails (FeatureID, SytleID) VALUES ('$feature', '$StyleID')";
$result1 = mysql_query($sql,$linkID)or die(mysql_error());

if ($result1 == true)
{
}
else
{
print "Not able to add feature";
}
}
}

it is reading the $StyleID but no the $feature

The codse on the previous page is as so.

<p align="right">1
<input type="checkbox" name="feature[]" value="Full poron foam sock" />
2
<input type="checkbox" name="feature[]" value="Poron foam inserts built into sock" /> and is posted across.

Any help appreciated

Posted: Fri Mar 05, 2004 11:14 am
by JayBird
What is your form method?

Change your checkbox names to features[] (notice the s)

so your code will be

Code: Select all

foreach($_POST['features'] as $feature) { 
	print "$feature"; 
	if (!empty($feature) == true) { 
		$sql = "INSERT INTO tblfeaturedetails (FeatureID, SytleID) VALUES ('$feature', '$StyleID')"; 
		$result1 = mysql_query($sql,$linkID)or die(mysql_error()); 
	}
}
Mark

Posted: Fri Mar 05, 2004 12:34 pm
by nutstretch
my form method is POST

MY php version is 4.3.3

I am getting this message now

Warning: Invalid argument supplied for foreach() in c:\program files\apache group\apache\htdocs\newwebsite\upload.php on line 81

Does this mean that foreach is the wrong method for 4.3.3

Posted: Fri Mar 05, 2004 12:40 pm
by markl999
If your form field is called 'feature' then you want :
foreach($_POST['feature'] as $feature) {
rather than
foreach($_POST['features'] as $feature) {

Posted: Fri Mar 05, 2004 12:43 pm
by nutstretch
both are features

Posted: Fri Mar 05, 2004 12:47 pm
by markl999
Then the error implies that 'features' wasn't posted, or isn't an array.
What does a var_dump($_POST); show? (and make sure the form names are 'features[]' not just 'features'.