Asked  7 Months ago    Answers:  5   Viewed   38 times

Basically my question is the following, how can i select on "Checked" checkbox's while doing a $_POST request in PHP, currently i have the checkbox's doing an array as shown below.

<input type="checkbox" value="1" name="checkbox[]">
<input type="checkbox" value="2" name="checkbox[]">
<input type="checkbox" value="2" name="checkbox[]">
<input type="checkbox" value="3" name="checkbox[]">

I want to be able to do something like this

foreach(CHECKED CHECKBOX as CHECKBOX) {
   echo CHECKBOX VALUE;
}

I've tried doing similar to that and it's not echoing anything.

 Answers

58
foreach($_POST['checkbox'] as $value) {

}

Note that $_POST['checkbox'] will only exist if at least one checkbox is checked. So you must add an isset($_POST['checkbox']) check before that loop. The easiest way would be like this:

$checkboxes = isset($_POST['checkbox']) ? $_POST['checkbox'] : array();
foreach($checkboxes as $value) {
    // here you can use $value
}
Wednesday, March 31, 2021
 
macha
answered 7 Months ago
93

You cannot redirect POST requests. As simple as that. Any redirect will always turn into a GET request.

If you want to receive POST data, then send that data to another page, you have two choices:

  • if both pages are on the same server, use sessions to save the data server-side, don't make the client carry it over
  • if the destination is on another server and you need to send the client there together with the data, set up another intermediate form like you are
Wednesday, March 31, 2021
 
Fanda
answered 7 Months ago
60

You can read the raw post data. For example:

<fieldset>
    <legend>Data</legend>
    <?php
    $data = file_get_contents("php://input");
    echo $data."<br />";
    ?>
</fieldset>

<fieldset>
    <legend>Form</legend>
    <form method="post" action="formtest.php">
        <input type="checkbox" value="val1" name="option"/><br />
        <input type="checkbox" value="val2" name="option"/><br />
        <input type="submit" />
    </form>
</fieldset>

Check both boxes and the output will be:

option=val1&option=val2

Here's a live demo. All you have to do then is to parse the string yourself, into a suitable format. Here's an example of a function that does something like that:

function parse($data)
{
    $pairs = explode("&", $data);

    // process all key/value pairs and count which keys
    // appear multiple times
    $keys = array();
    foreach ($pairs as $pair) {
        list($k,$v) = explode("=", $pair);
        if (array_key_exists($k, $keys)) {
            $keys[$k]++;
        } else {
            $keys[$k] = 1;
        }
    }

    $output = array();
    foreach ($pairs as $pair) {
        list($k,$v) = explode("=", $pair);
        // if there are more than a single value for this
        // key we initialize a subarray and add all the values
        if ($keys[$k] > 1) {
            if (!array_key_exists($k, $output)) {
                $output[$k] = array($v);
            } else {
                $output[$k][] = $v;
            }
        } 
        // otherwise we just add them directly to the array
        else {
            $output[$k] = $v;
        }
    }

    return $output;
}

$data = "foo=bar&option=val1&option=val2";

print_r(parse($data));

Outputs:

Array
(
    [foo] => bar
    [option] => Array
        (
            [0] => val1
            [1] => val2
        )

)

There might be a few cases where this function doesn't work as expected though, so be careful.

Wednesday, March 31, 2021
 
brombeer
answered 7 Months ago
52

Just a guess:

Your $json variable is a string. You'll need to convert it to an object using json_decode to iterate through the object:

$json_obj = json_decode( $json );
foreach( $json_obj as $obj )
{
  //stuff
}
Saturday, May 29, 2021
 
koenHuybrechts
answered 5 Months ago
51
    $ownership = nl2br($_POST['product-types-owned']); 

product-types-owned is going to be an array of the checkbox values that were selected. You'll need to implode that before you do anything else:

$ownership = nl2br(implode(',', $_POST['product-types-owned']));

Right now you're trying to nl2br on an array, which won't work. nl2br expects a string, so php will type cast the array into its default string representation, which is literally the word Array.

Saturday, August 21, 2021
 
Zigglzworth
answered 2 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :