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

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

nutstretch
Forum Contributor
Posts: 104
Joined: Sun Jan 11, 2004 11:46 am
Location: Leicester

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

Post 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
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post 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
nutstretch
Forum Contributor
Posts: 104
Joined: Sun Jan 11, 2004 11:46 am
Location: Leicester

Post 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?
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post 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
nutstretch
Forum Contributor
Posts: 104
Joined: Sun Jan 11, 2004 11:46 am
Location: Leicester

Post by nutstretch »

cheers
so that collects each array part in turn from the previous form
allowing you to 'do your stuff'
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

it surely does :)
nutstretch
Forum Contributor
Posts: 104
Joined: Sun Jan 11, 2004 11:46 am
Location: Leicester

Post by nutstretch »

Many thanks for you polite (not saying how thick i am) and speedy replies

Angie
nutstretch
Forum Contributor
Posts: 104
Joined: Sun Jan 11, 2004 11:46 am
Location: Leicester

Post by nutstretch »

do I have to put the [] brackets in the $_POST['feature[]'];
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

nutstretch wrote:do I have to put the [] brackets in the $_POST['feature[]'];
No
nutstretch
Forum Contributor
Posts: 104
Joined: Sun Jan 11, 2004 11:46 am
Location: Leicester

Post 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
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post 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
nutstretch
Forum Contributor
Posts: 104
Joined: Sun Jan 11, 2004 11:46 am
Location: Leicester

Post 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
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

If your form field is called 'feature' then you want :
foreach($_POST['feature'] as $feature) {
rather than
foreach($_POST['features'] as $feature) {
Last edited by markl999 on Fri Mar 05, 2004 12:47 pm, edited 1 time in total.
nutstretch
Forum Contributor
Posts: 104
Joined: Sun Jan 11, 2004 11:46 am
Location: Leicester

Post by nutstretch »

both are features
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post 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'.
Post Reply