Validating multiple inputs with a shorter code in PHP

Gragas Incoming

The below code shows the process of validating 2 input fields. At the end i return in Json the Boolean result of each input.

Imagine i will do the following 20 more times, the code is large. How can an i perform the same procedure with shorter code?

$res =false;
$res2 = false;
    if(isset($_POST['Ans1'])) //if user checked ans1 radio
        {
          if($_POST['Ans1'] == "x divides y") #correct answer
          {
                $res = true;
          }
          else{#wrong answer
              $res = false;
          }
        }
        else{ #not checked
            $res = false;  
        }


        if(isset($_POST['Ans2'])) //if user checked ans2 radio
        {
          if($_POST['Ans2'] == "x greater than y") #correct answer
          {
                $res2 = true;
          }
          else{#wrong answer
              $res2 = false;
          }
        }
        else{ #not checked
            $res2 = false;
        }

        echo json_encode(['an1'=>$res,'an2'=>$res2]); 
Akshay Gohil

You can use a function to check answer like this one:

function checkAnswer($key, $val) {
    $mapping = [
        'Ans1' => 'x divides y',
        'Ans2' => 'x greater than y'
    ];

    return $mapping[$key] == $val;
}

You will need a mapping of answers. You can then loop the POST array to check the answers; if POST only has answers.

$result = [];
foreach($_POST as $key => $val) {
    $res = checkAnswer($key, $val);
    $result[$key] = $res;
}
echo json_encode($result);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

validating multiple inputs at the same time

From Dev

Is it possible to make this PHP code shorter?

From Dev

Validating multiple "array named" file inputs and dropdowns in JQuery Validate

From Dev

Generating Multiple Lags in R with shorter code

From Dev

Validating Inputs entered in REDQueryBuilder

From Dev

Validating TinyMCE for empty inputs

From Dev

Validating inputs using Optional

From Dev

Validating string inputs

From Dev

Validating TinyMCE for empty inputs

From Dev

How to validate multiple inputs with PHP

From Dev

How to validate multiple inputs with PHP

From Dev

PHP Multiple file upload with multiple inputs

From Dev

Submitting form after validating inputs

From Dev

From Array with Multiple Inputs to if statement with varnames PHP

From Dev

Making monadic code shorter

From Dev

Is there a shorter way to write this code?

From Dev

Making monadic code shorter

From Dev

Shorter code for capitalize function

From Dev

Shorter PHP Script

From Dev

Validating repeating inputs inside an AngularJS form

From Dev

Validating Dynamically Added Form Inputs - Vanilla JS

From Dev

Validating different types of form inputs with criterias

From Dev

Modifying the Caffe C++ prediction code for multiple inputs

From Dev

Is there any shorter code or easier way to add elements into multiple arrays of different sizes?

From Dev

Schematron validating multiple elements

From Dev

Validating multiple files in array

From Dev

Javascript validating multiple fields

From Dev

Validating multiple files in array

From Dev

Java - validating a subject Code