-2

More clarification: I have a checkbox with name="agree" in my register form, and the user must mark this value="agree" to complete the process of registration, if this checkbox not marked an error message will appear to the user. As a test I tried to submit the form without mark this checkbox but I found this INDEX error appeared saying: [ Notice: Undefined index: agree in ] even the error message for the user has appeared too.

I have a checkbox and radio fields in my form, these fields are optional. So when I submit my form and these values are empty I get an INDEX error. Is there any way to avoid that error without using error_reporting(0) fuction:

<form action="" method="post">
    <div class="checkbox">
        <label>
             <input type="checkbox" name="agree" value="agree">
        </label>
    </div>
</form>

<?php
   if (isset($_POST['submit'])) {
      $agree = $_POST['agree'];
   }
?>
Mahmoud Yhya
  • 31
  • 1
  • 9

2 Answers2

1

You only checked the submit button request field. But the agree checkbox needs a separate isset/empty if you want to suppress notices.

An alternative, which also shouldn't be used thoughtlessly, is the @() error display omitter. It's universally despised, but more concise:

$agree = @($_POST["agree"]);

Take in mind that unlike isset/empty it doesn't completely obliterate notices. It merely prevents the default notice display. They still show up in logs…

Which is rarely what you want. But if it's an actual (TOS?) "agree" checkbox, it might well be a practical use case. (- Otherwise you had to implement a custom log scheme/table for monitoring user preferences and terms of service haters. You know, if that's what it's about.)

There's also the upcoming PHP7 null coalesce operator, $agree = $_POST["agree"] ?? false; btw. It's likewise readable but fully suppresses notices like the isset/empty combo.

Community
  • 1
  • 1
mario
  • 144,265
  • 20
  • 237
  • 291
0

You should use a conditional with the isset or empty function. Assuming you use agree in as a bit value you could use the ternary operator to set the value like this:

<form action="" method="post">
    <div class="checkbox">
        <label>
             <input type="checkbox" name="agree" value="agree">
        </label>
    </div>
</form>

<?php
   if (isset($_POST['submit'])) {
      $agree = !empty($_POST['agree']) ? 1: 0;
   }
?>

Per the empty manual,

No warning is generated if the variable does not exist. That means empty() is essentially the concise equivalent to !isset($var) || $var == false.

chris85
  • 23,846
  • 7
  • 34
  • 51
  • What happens, what do you get for a message? What is the usage of `$agree` later? – chris85 Sep 21 '15 at 01:32
  • I have a checkbox with name="agree" in my register form, and the user must mark this value="agree" to complete the process of registration, if this checkbox not marked an error message will appear to the user. As a test I tried to submit the form without mark this checkbox but I found this INDEX error appeared saying: [ Notice: Undefined index: agree in ] even the error message for the user has appeared too. – Mahmoud Yhya Sep 21 '15 at 15:31