0

I have a form with multiple radio buttons, they all seem to work fine until i try to do error handling.

CODE HTML:

 <form action="" method="POST">
  <input type="radio" name="gender" value="male" checked> Male<br>
  <input type="radio" name="gender" value="female"> Female
   <input type="submit" value="Submit">
</form> 

php:

if (strip_tags(trim(isset($_POST['gender']))) == NULL) {
        $error[] = ' Error: please select male or female ';
    } else { //if it is set then $uGender should hold post value 
        $uGender = strip_tags(trim($_POST['gender']));

    }

I tried to catch if the radio button male or female have been set usingisset()` when I try to use it pushes out undefined variable or undefined index. It will still post without having any value which leads to a SQL error as the Gender field by default cannot be a NULL value.

Another issue: it posts everything to the database and leaves the gender field empty even though there is an error handler?

Sumithran
  • 6,217
  • 4
  • 40
  • 54
JamesBond
  • 312
  • 2
  • 17
  • instead of `if (strip_tags(trim(isset($_POST['gender']))) == NULL) {` just `if (isset($_POST['gender'])) {` – Alex Jul 23 '18 at 17:43
  • 2
    Need a NOT `if (!isset($_POST['gender'])) {` – AbraCadaver Jul 23 '18 at 17:47
  • Do not Access `$_post` https://stackoverflow.com/questions/19767894/warning-do-not-access-superglobal-post-array-directly-on-netbeans-7-4-for-ph – sanoj lawrence Jul 23 '18 at 17:51
  • `if (strip_tags(trim(isset($_POST['gender'])))` can't be NULL here as you already checked one value by default. You can check whether male or female is selected. So you can bring it's reference for next immediate conditional input validation, if necessary. – Klanto Aguntuk Jul 23 '18 at 18:24

3 Answers3

0

You can simply use the following code to apply that validation:

    if ( isset($_POST['gender'])==false ) {
        $error[] = ' Error: please select male or female ';
    } else { //if it is set then $uGender should hold post value 
        $uGender = strip_tags(trim($_POST['gender']));

    }
Ahmed Numaan
  • 1,034
  • 1
  • 10
  • 26
0

try this

if (isset ($_POST['gender']) == 0) {
        $error[] = ' Error: please select male or female ';
    } else { //if it is set then $uGender should hold post value 
        $uGender = strip_tags(trim($_POST['gender']));

    }
Sumithran
  • 6,217
  • 4
  • 40
  • 54
0

Thanks to @AbraCadver, @Alex & @Sumithran. I used the combined results to figure out the fix.

To make sure the radio button is unable to post or display any SQL cannot be NULL errors.

First you will need to check if the variable is set using isset() then simply create another handler for if the variable is not set.

Below is the fix that worked:

if (!isset($_POST['Gender'])) {

    } else {
        $uGender = strip_tags(trim($_POST['Gender']));
    }

// then I have the following to double check if its really set or not

if (isset($uGender) == 0) {
        $error[] = ' Please Select either Male or Female !';
    }
JamesBond
  • 312
  • 2
  • 17