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
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

Link to comment
Share on other sites

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])

 

 

Link to comment
Share on other sites

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);
}

Link to comment
Share on other sites

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.

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.