PHP keeps telling me value is empty

I have it setup in my PHP script that if a value is empty, say so. I keep getting a message in the console saying the value is empty, but I can't seem to figure out why because it looks like it has a value to me.

Here is the PHP

$percentageOfMessages = $_POST["percentageOfMessages"];
$RemoveDeletedAccounts = $_POST["RemoveDeletedAccounts"];
$RemoveNoReply = $_POST["RemoveNoReply"];
$RemoveNoResponse = $_POST["RemoveNoResponse"];
$mintchPercent = $_POST["mintchPercent"];
$minDistance = $_POST["minDistance"];
$maxDistance = $_POST["maxDistance"];

$blacklistUsernames = $_POST["blacklistUsernames"];
$pickText = $_POST["pickText"];
$userEmail = $_POST["userEmail"];

//$captcha = $_POST["captcha"];
//$num1 = $_POST["num1"];
//$num2 = $_POST["num2"];


if (empty($percentageOfMessages)) {
    echo "percentageOfMessages";

}elseif (empty($mintchPercent)) {
    echo "mintchPercent";

}elseif (empty($minDistance)) {
    echo "minDistance";

}elseif (empty($maxDistance)) {
    echo "maxDistance";

}elseif (empty($pickText)) {
    echo "pickText";

}elseif (empty($userEmail)) {
    echo "userEmail";

}else {

Here is the javascript/jquery that involved minDistance

    $('#minDistance').keyup(function() {
        if (this.value != this.value.replace(/[^0-9\.]/g, '')) {
            this.value = this.value.replace(/[^0-9\.]/g, '');
        } else if ($(this).val() > 500) {
            $(this).val('500');
        } else if ($(this).val() < 0) {
            $(this).val('0');
        }
    });

    $('#minDistance').blur(function() {
        if ($(this).val() > $('#maxDistance').val()) {
            $(this).val('0');
        }
    });

    function sendForm(e) {
        e.preventDefault();
        var formData = new FormData();

        formData.append('percentageOfMessages', $('#percentageOfMessages').val());

        if ($('RemoveDeletedAccounts').prop('checked')) {
            formData.append('RemoveDeletedAccounts', "1");
        } else {
            formData.append('RemoveDeletedAccounts', "0");
        }

        if ($('RemoveNoReply').prop('checked')) {
            formData.append('RemoveNoReply', "1");
        } else {
            formData.append('RemoveNoReply', "0");
        }

        if ($('RemoveNoResponse').prop('checked')) {
            formData.append('RemoveNoResponse', "1");
        } else {
            formData.append('RemoveNoResponse', "0");
        }

        formData.append('minMatchPercent', $('#minMatchPercent').val());
        //OTHER STUFFFFFFFFF
 $.ajax({
            type: 'POST',

            xhr: function() { // Custom XMLHttpRequest
                var myXhr = $.ajaxSettings.xhr();
                if (myXhr.upload) { // Check if upload property exists
                    //myXhr.upload.addEventListener('progress',progressHandlingFunction, false); // For handling the progress of the upload
                }
                return myXhr;
            },
            cache: false,
            processData: false,
            contentType: false,
            data: formData,
            url: 'addAccounts.php',
            success: function(data) {
                console.log(data);
            },
            error: function(xhr, err) {
                console.log("readyState: " + xhr.readyState + "\nstatus: " + xhr.status);
                console.log("responseText: " + xhr.responseText);
            }
        });

    }

And here is the HTML

<div class="row uniform">
    <div class="6u 12u$(xsmall)">
        <input type="text" name="minDistance" id="minDistance" value="0" />
    </div>
    <div class="6u 12u$(xsmall)">
        <input type="text" name="maxDistance" id="maxDistance" value="500" />

    </div>
</div>

Answers 1

  • Returns FALSE if var exists and has a non-empty, non-zero value. Otherwise returns TRUE.
    The following things are considered to be empty:

    • "" (an empty string)
    • 0 (0 as an integer)
    • 0.0 (0 as a float)
    • "0" (0 as a string)
    • NULL
    • FALSE
    • array() (an empty array)
    • $var; (a variable declared, but without a value)

    Code sample:

    // Evaluates to true because $var is empty
    if (empty($var)) {
        echo '$var is either 0, empty, or not set at all';
    }
    // Evaluates as true because $var is set
    if (isset($var)) {
        echo '$var is set even though it is empty';
    }
    

    According to manual 0 or "0" will be considered empty you can use isset() instead.


Related Articles