lots of check boxes! is there an quick way of entering
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
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
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
Just name your check boxes like this
Then, all the values will be available in an array called fetaures which you can iterate thorugh when the form has been submitted.
Mark
Code: Select all
<input type="checkbox" name="featuresї]" value="some_feature">Mark
-
nutstretch
- Forum Contributor
- Posts: 104
- Joined: Sun Jan 11, 2004 11:46 am
- Location: Leicester
something like
Change $_POST to $_GET depending on your form method
Mark
Code: Select all
foreach($_POST['features'] as $feature) {
// Do your stuff
}Mark
-
nutstretch
- Forum Contributor
- Posts: 104
- Joined: Sun Jan 11, 2004 11:46 am
- Location: Leicester
-
nutstretch
- Forum Contributor
- Posts: 104
- Joined: Sun Jan 11, 2004 11:46 am
- Location: Leicester
-
nutstretch
- Forum Contributor
- Posts: 104
- Joined: Sun Jan 11, 2004 11:46 am
- Location: Leicester
-
nutstretch
- Forum Contributor
- Posts: 104
- Joined: Sun Jan 11, 2004 11:46 am
- Location: Leicester
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
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
What is your form method?
Change your checkbox names to features[] (notice the s)
so your code will be
Mark
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());
}
}-
nutstretch
- Forum Contributor
- Posts: 104
- Joined: Sun Jan 11, 2004 11:46 am
- Location: Leicester
If your form field is called 'feature' then you want :
foreach($_POST['feature'] as $feature) {
rather than
foreach($_POST['features'] as $feature) {
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