Jump to content

Inserting multiple checkboxes into PHP


jw12345

Recommended Posts

Let me say upfront I'm not php or mysql expert - enough to be really dangerous.

 

I have a form on my website that contains three different checkboxes.

 

I have named all 3 of those checkboxes the same thing, with different values. My hope by doing so, when the form is submitted it would insert all the different values checked into the column "type" into mySQL database.

 

But it doesn't... only take the last one checked.

 

So, my question is this - is there any php code I can put in to connect all three of those checkboxes to insert properly into the database?

 

Not sure what the best way is to solve this.

Link to comment
https://forums.phpfreaks.com/topic/47290-inserting-multiple-checkboxes-into-php/
Share on other sites

<input type="checkbox" name="cb[]" value="some value" />

 

Will put them into an array IE:

 

if (isset($_POST['cb']) && is_array($_POST['cb'])) {
    foreach ($_POST['cb'] as $value) {
        print $value . "<br />";
    }
}

 

Should work, not sure but yea.

Wow dude, read what we are saying lol

 

<input type=checkbox name="my_checkbox[]" value="type1">
<input type=checkbox name="my_checkbox[]" value="type2">
<input type=checkbox name="my_checkbox[]" value="type3">

<?php
if (isset($_POST['cb']) && is_array($_POST['cb'])) {
    foreach ($_POST['cb'] as $type) {
        $types .= "type='" . $type . "' ";
    }
}
?>

 

Something along those lines.

My bad - like I said, not a pro at all, but should have read more carefully. My apologies.

 

I did copy & paste that directly in there as you had it... didn't alter a thing, and it now it's just blank in the the mySQL database...

 

Will read that link you sent, thank Wildbug... and thanks to everybody for trying to help. I'll get this solved one way or another

// the form
<input type=checkbox name="type[]" value="type1">
<input type=checkbox name="type[]" value="type2">
<input type=checkbox name="type[]" value="type3">

<?php
// processing the form

if (isset($_POST['type']) && is_array($_POST['type'])) {
    $types = join (', ', $_POST['type'];
}
else $types = '';

// write $types to the db

?>

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.