-2

This is my form and categories comes from the database with while loop. I want to insert the checked inputs only in to database.

How can i detect which checkbox is selected ?

<form action="account.php" method="POST">
    <ul class="account-info">
        <li>Category1 : <input type="checkbox" value="val1" name="cat1"></li>
        <li>Category2 : <input type="checkbox" value="val2" name="cat1"></li>
        <li>Category3 : <input type="checkbox" value="val3" name="cat1"></li>
        <!-- while continues -->
        <li>Category100 : <input type="checkbox" value="val100" name="cat1"></li>
    </ul>
    <input type="submit" value="submit" />
</form>
  • Only selected checkboxes are passed in the POST array. – Jay Blanchard Jul 10 '15 at 15:55
  • you might be thinking of a radio button. (because you ask which ONE is selected, and named them all cat1). What you have in your code are checkboxes, and each one needs a unique name, and then you can check each one in php when the form is posted. – RightClick Jul 10 '15 at 15:55
  • @RightClick They don't need to be uniquely named, check my answer. – Styphon Jul 10 '15 at 15:57
  • possible duplicate of [Getting checkbox values on submit](http://stackoverflow.com/questions/18421988/getting-checkbox-values-on-submit) – kittykittybangbang Jul 10 '15 at 15:59
  • @JayBlanchard its okey but i dont know how to detect checked inputs. Generally im using it : `$String = $_POST["postedstring"];` But im getting checkboxes from database with while loop. how can i get these values in account.php – soner kaya Jul 10 '15 at 15:59
  • I would like to suggest removing the tags: `php`, `mysql` and `phpmyadmin` (and maybe also `html5`). This is basic html (form posting). You can test this by creating a pure html form and checking what URL the browser triggers when you hit "submit". – Risadinha Jul 10 '15 at 16:01
  • @Styphon thanks for reminding me of that, I forgot you could do that because I usually care which one was clicked. I guess if you have all distinct values and don't care which checkbox they came from that would work. – RightClick Jul 10 '15 at 16:03

4 Answers4

2

In account.php only the check boxes will be posted. As you've named them all the same though, only 1 will be posted, the last checkbox. If you want them to have the same name and come through as an array you need to add [] after the name, like this:

<input type="checkbox" value="val100" name="cat1[]">

Then in your account.php where they are submitted you can do this:

foreach($_POST['cat1'] as $val)
{
    echo "$val<br>";
}

That will echo out the values of all the checked boxes.

Styphon
  • 10,304
  • 9
  • 52
  • 86
  • Its okey but cat2 ? or cat75 i dont know how many categories will be added. – soner kaya Jul 10 '15 at 16:03
  • @sonerkaya using the `[]` will generate and array of only the checked boxes. so if they check cat2 and cat75, only those 2 will be added to the array. so using a foreach loop it will loop twice allowing you to use only those 2 values. – iam-decoder Jul 10 '15 at 16:07
0

You can check it like that: This code will check which categories are activated assuming that your database returns categories from 0 to x

 $i = 0; 
    while(true){
    if ($_POST["cat".$i."]) {   
        //category activated
    } 
    else { 
        //no category found -> loop ends
        break;
    }
    $i++;
    }
Coder55
  • 559
  • 5
  • 17
  • Its okey but cat2 ? or cat75 i dont know how many categories will be added. I cant write codes manually because categories comes from database – soner kaya Jul 10 '15 at 16:03
0

change the name="cat1" to name="cat[1][]"

in your account.php page

foreach($_POST['cat'] as $category){ 
    foreach($category as $value){
        echo $value;
    }
}
cmorrissey
  • 8,493
  • 2
  • 23
  • 27
0

First off, you have your options set up as if they were radio buttons all having the same name turning into 1 value out of the 3 possible. If you want them to be conventional checkboxes you have to give them separate names.

in your PHP you would check if the array_key_exists for the checkbox name in question, this is how I usually translate the checkbox fields in a form to be easier to use:

<?php
    $checked = array(
        'cat1' => array_key_exists('cat1', $_POST),
        'cat2' => array_key_exists('cat2', $_POST),
        'cat3' => array_key_exists('cat3', $_POST)
    );
    print_r($checked);
    exit;
?>
iam-decoder
  • 2,554
  • 1
  • 13
  • 28