Jump to content

Multiple Checkbox Selection in form


phpbeginner

Recommended Posts

I am using a form to update my database and within the form there are a large number of fields which I update on GO.

 

Within that form one of the fields I am updating, I would like to have a multiple selection choice for that field as below.

 

<input type=checkbox name=flooring[] value=Hardwood> Hardwood
<input type=checkbox name=flooring[] value=Carpet> Carpet
<input type=checkbox name=flooring[] value=Linoleum> Linoleum

 

Now when I submit go there are all kinds of things I am inserting including flooring

 

$query = "INSERT INTO styles (flooring) values($flooring)

 

But in the database all that is inserted is ARRAY. Also, what is the best field type to insert multiple selections in?

 

Thanks In Advance

Link to comment
https://forums.phpfreaks.com/topic/122046-multiple-checkbox-selection-in-form/
Share on other sites

it sounds like you want to

 

$flooringTypes = join (',', $floorings);
$query = "INSERT INTO styles (flooring) values($flooringTypes)";

 

I'd recommend

foreach ($floorings as $flooringType)
{
    $query = "INSERT INTO styles (flooring) values($flooringType)";
}

 

where the flooring style table has an autoinc id

Form.....

 

<input type=checkbox name=flooring[] value=Hardwood> Hardwood
<input type=checkbox name=flooring[] value=Carpet> Carpet
<input type=checkbox name=flooring[] value=Linoleum> Linoleum

 

Then

 

if ($action=="go")
                                 
                   		{
                        $flooringCount = count($flooring);
                        for ($i=0; $i < $flooringCount; $i++)

                       $query = "INSERT INTO styles (flooring) values($flooring[$i])

 

 

of course, you need to execute the queries

 

foreach ($flooring as $flooringType)
{
    $query = "INSERT INTO styles (flooring) values('$flooringType')";
    mysql_query($query);
}

 

or in your code

for ($i=0; $i < $flooringCount; $i++)
{
     $query = "INSERT INTO styles (flooring) values('$flooring[$i]')";
     mysql_query($query);
}

Hi Barand, I got it to work using the following.

 

$flooring=$_POST["flooring"];
        $flooringstr="";
        for($i=0;$i<sizeof($flooring);$i++){
        $flooringstr .= $flooring[$i]." ";}

 

and changing value to ($flooringstr).

 

NOW, I having a tremendous amount of difficulty with populating the checkboxes on my edit page.

 

I am inserting the values in a text type field, and as you can see, seperated by a space. Any ideas on this one?

 

Appreciate your help.

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.