Viewed   52 times

I have an HTML form:

<form action='process.php' method='post'>
  <input type='checkbox' name='check_box_1' /> Check me!<br>
</form>

Here is a section from the PHP script process.php:

echo (isset($_POST['check_box_1']))?'Set':'Not set';

The output of the script when the checkbox is set is

Set

But when the checkbox is not set, the output is:

Not set

Why is this? This seems like a very poor design because my PHP script checks a number of $_POST variables to make sure they were passed along to the script. When the $_POST['check_box_1'] value is not set, then how do I know whether the script failed to pass along the value or the checkbox was just not set?

 Answers

4

If you want to overcome this design feature, try this:

<input type="hidden" name="check_box_1" value="0" />
<input type="checkbox" name="check_box_1" value="1" />

This way, you'll always have $_POST['check_box_1'] set in the callback page, and then you can just check its value to see whether they checked it or not. The two inputs have to be in that order though, since the later one will take precedence.

Monday, October 24, 2022
3

I found that I can work around this by adding the following to the apache config on Windows:

SetEnvIf Authorization "(.*)" HTTP_AUTHORIZATION=$1

This adds a HTTP_AUTHORIZATION key to the $_SERVER global, just as if the Authorization header had been passed through by Apache.

Saturday, November 26, 2022
 
1

Give the checkboxes names as array like

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

And after submit try like

$checked_arr = $_POST['checkbox'];
$count = count($checked_arr);
echo "There are ".$count." checkboxe(s) are checked";

Note : And based on the method that your form submit using...whether it is $_GET or $_POST you need to use $_POST['checkbox'] for POST method and $_GET['checkbox'] for the GET method.

Saturday, December 24, 2022
 
sgeddes
 
5

After searching here and there, I discovered that the post body data gets into the $_POST variables only when you send them as a form -i.e., application/x-www-form-urlencoded- (I guess that is what $_POST stands for, not the method used for the request). Correct me if I'm saying something that isn't correct.

When using a Content-Type of application/json, code like the following does the trick:

$data = json_decode(file_get_contents('php://input'), true);

$userId = $data["userId"];
$password = $data["password"];

I guess this is very basic stuff, but then again, my knowledge of HTTP is very limited...

Saturday, August 13, 2022
 
volker
 
3

Three words: array it up!

Observe this code:

<form action="order.php" method="post">
    <p>
        Coffee: <br>

        <!-- This ensures false is submitted if the cappuccino box is unticked -->
        <input type="hidden" name="coffee[cappuccino][selected]" value="0">
        <label><input type="checkbox" name="coffee[cappuccino][selected]" value="1"> Cappuccino</label>
        <input type="number" name="coffee[cappuccino][qty]" size="2"><br>

        <!-- This ensures false is submitted if the espresso box is unticked -->
        <input type="hidden" name="coffee[espresso][selected]" value="0">
        <label><input type="checkbox" name="coffee[espresso][selected]" value="1"> Espresso</label>
        <input type="number" name="coffee[espresso][qty]" size="2"><br>
    </p>

    <p>[...]</p>

    <p>
        <label><input type="radio" name="dine" value="in"> Dine in</label>
        <label><input type="radio" name="dine" value="out"> Take out</label>
    </p>

    <p><input type="submit" value="submit" name="submit"></p>
</form>

When this submits, the inputs are submitted as an associative array as this:

array (size=3)
  'coffee' => 
    array (size=2)
      'cappuccino' => 
        array (size=2)
          'selected' => string '1' (length=1)
          'qty' => string '4' (length=1)
      'espresso' => 
        array (size=2)
          'selected' => string '1' (length=1)
          'qty' => string '3' (length=1)
  'dine' => string 'in' (length=2)
  'submit' => string 'submit' (length=6)

As you can see, each coffee is now being submitted as an array with the type of coffee being a key that also has an array of its own with two further keys: selected and qty.

The selected determines if the checkbox for that coffee was ticked (1) or not (0) and the qty holds the user input.

This pretty much does your validation since each quantity input now belongs to the individual coffee.

I hope I've understood your dilemma correctly and this answers your question or give a you an idea of how to go on about doing this.

Enjoy :)

Sunday, September 18, 2022
 
gub
 
gub
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :