When a form is submitted to a PHP script, the information from that form is automatically made available to the script.
- The $_POST variable is used to access this information. This predefined $_POST variable is used to collect values from a form sent with method=”post”.
- The $_POST variable is an associative array of variables passed to the current script via the HTTP POST method
- Information sent from a form with the POST method is invisible to others and has no limits on the amount of information to send.
NOTE: There is an 8 MB max size for the POST method, by default (can be changed by setting the post_max_size in the php.ini file).
/*** The code snippet uses an if condition, $_POST variable, “is_array” function, and the explode and implode functions***/
if (!$_POST[‘maintenance_type’]) {
$maint = ”;
} else {
if (is_array($_POST[‘maintenance_type’]) == false) {
$maint = explode(‘,’, $_POST[‘maintenance_type’]);
$limit = ‘1=1’;
} else {
$maint = $_POST[‘maintenance_type’];
$limit = ‘1=1’;
}
}
if ($maint <> “”) {
$sqlmaint = “AND fr.maint_type IN (‘” . implode(“‘, ‘”, $maint) . “‘)”;
}
/*** Explanation of the code snippet **/
1. The “If” condition checks whether any value collected by the $_POST variable. If there is no value in the $_POST variable, then initialize the variable “$maint” with NULL.
if (!$_POST[‘maintenance_type’]) {
$maint = ”;
}
2. Else part: If $_POST variable has value (Else part), it uses the “is_array” function to check whether $_POST[‘maintenance_type’] is an array or not.
i. If the $_POST variable is not an array, we convert the string to an array “$maint” using the explode function and the limit is set to “1=1”.
$maint = explode(‘,’ , $_POST[‘maintenance_type’]);
$limit = ‘1=1’;
ii. If the $_POST variable contains a list of values (array) then it is assigned to an array variable “$maint” and the limit is set to “1=1”.
$maint = $_POST[‘maintenance_type’];
$limit = ‘1=1’;
3. Now, if the “$maint” variable is NOT NULL then we should form the “AND” condition for our query. This is done using the implode function. We create a string out of the “$maint” array and assign it to the String variable “$sqlmaint”.
if ($maint <> “”) {
$sqlmaint = “AND fr.maint_type IN (‘” . implode(“‘, ‘”, $maint) . “‘)”;
}