Jump to content

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

?>

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.