Wicked Wicked - 7 months ago 22
PHP Question

For each, if any empty: no whitespace

I'm working with a

foreach
loop. If condition returns empty, it should
echo
something. But I was getting the opposite effect. I am trying to make sure the POST actually contains data, and not just
whitespaces
. Here is what I have so far:

<?php
$transponame = $_POST['transponame'];
$transpoid = $_POST['transpoid'];

foreach( $transponame as $key => $n ) {
$name = mysql_real_escape_string($name);
$name = htmlentities($name, ENT_QUOTES);
$n = mysql_real_escape_string($n);
$n = htmlentities($n, ENT_QUOTES);
$transpoid[$key] = htmlentities(strip_tags($transpoid[$key]),ENT_QUOTES);
if (!empty($n) && !empty($transpoid[$key])) {
echo "its empty";
};

} ?>

Answer

Variable with white space is not empty. So you have to trim the value.

<?php
$transponame = $_POST['transponame'];
$transpoid = $_POST['transpoid'];

foreach( $transponame as $key => $n ) {
    $name = mysql_real_escape_string($name);
    $name = htmlentities($name, ENT_QUOTES);
    $n = mysql_real_escape_string($n);
    $n = htmlentities($n, ENT_QUOTES);
    $transpoid[$key] = htmlentities(strip_tags($transpoid[$key]),ENT_QUOTES);
if (empty(trim($n)) || empty(trim($transpoid[$key]))) { 
echo "its empty";
};  

} ?>