Php Isset Not Working Properly On Form, With Loops And Arrays
This is honestly the most finicky and inept language I've ever coded in. I'll be glad when this project is good and over with. In any case I have to us PHP so here's my question.
Solution 1:
Instead of checking !isset()
, use empty()
. If the form posts an empty string, it will still show up in the $_POST
as an empty string, and isset()
would return TRUE
.
I've replaced your incremental for
loop with a foreach
loop, which is almost always used in PHP for iterating an array.
$out_data = array();
foreach ($form_dataas$key) {
if(empty($_POST[$key])) {
$out_data[$key] = "NO_DATA";
}
else {
$out_data[$key] = $_POST[$key];
}
}
Solution 2:
PHP's isset
returns TRUE
unless the variable is undefined or it is NULL
. The empty string ""
does not cause it to return FALSE
. empty()
will do exactly what you need, though.
http://php.net/manual/en/function.isset.php
isset() will return FALSE if testing a variable that has been set to NULL. Also note that a NULL byte ("\0") is not equivalent to the PHP NULL constant.
Returns TRUE if var exists and has value other than NULL, FALSE otherwise.
Post a Comment for "Php Isset Not Working Properly On Form, With Loops And Arrays"