PHP一直告诉我价值是空的

时间:2016-03-30 04:19:08

标签: javascript php jquery html

我在PHP脚本中设置了如果值为空,请说明。我一直在控制台中收到一条消息,说价值是空的,但我似乎无法找出原因,因为它看起来对我有价值。

这是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 {

这是涉及minDistance

的javascript / jquery
    $('#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);
            }
        });

    }

这是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>

1 个答案:

答案 0 :(得分:0)

  

如果var存在且具有非空的非零值,则返回FALSE。   否则返回TRUE   以下内容被认为是空的:

  • "" (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)

代码示例:

// 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';
}

根据manual 0"0"将被视为空,您可以使用isset()代替。