【问题标题】:PHP !isset() and empty() returns true for AJAX post data even if it is setPHP !isset() 和 empty() 为 AJAX 发布数据返回 true,即使它已设置
【发布时间】:2017-06-09 17:41:11
【问题描述】:

我想知道为什么 !isset($var)empty($var) 在这种情况下会返回 TRUE,即使它不是。

实际上,这个问题已经通过使用$var == NULL 解决了,但这不是问题所在。我读过类似的问题,但问题始终与数据类型有关;我确信我通过记录和转储每个变量获得了正确的数据类型,并将条件更改为$var == NULL,它可以工作并返回FALSE

编辑:这是 HTML:

<td><?php echo $match['partic1_name'] ?></td>
<td><input type="number" max="2" id="partic1_score" style="width: 80px;" class="form-control score-input" value="<?php echo $match['partic1_score']; ?>"></td>
<td><input type="number" max="2" id="partic2_score" style="width: 80px;" class="form-control score-input" value="<?php echo $match['partic2_score']; ?>"></td>
<td><?php echo $match['partic2_name'] ?></td>
<td><button id="<?php echo $match['id'] ?>" type="submit" class="btn btn-success score-submit"><span class="glyphicon glyphicon-ok"></span> Submit</button></td>

这里是 AJAX 部分(完整):

$(".score-submit").click(function () {
        var field1 = $(this).parent().parent().find("#partic1_score");
        var field2 = $(this).parent().parent().find("#partic2_score");
        var s1 = parseInt(field1.val(), 10);
        var s2 = parseInt(field2.val(), 10);
        var t = s1 + s2;

        if (t > 3 || t < 2) {
            alert("Invalid Scores. The score must be 2-0, 2-1, 0-2, 1-2, or 1-1");
        } else {
            var target = '<?php echo base_url("index.php/matches/submit/") ?>';
            var id = $(this).attr("id");
            var self = this;
            $.ajax({
                url: target,
                type: "POST",
                data: {'id': id, 'score1': s1, 'score2': s2},
                success: function(response) {
                    console.log(response);
                    if (response == 1) {
                        field1.prop('disabled', true);
                        field2.prop('disabled', true);
                        $(self).attr('disabled', 'disabled');
                    } else {
                        alert("Failed to do the operation.");
                    }
                },
                error: function() {
                    alert("Something went wrong. Please refresh the page before trying again.");
                }
            });
        }
    });

控制器:

public function submit() {
    $id = $this->input->post('id');
    $score1 = $this->input->post('score1');
    $score2 = $this->input->post('score2');
    echo $this->matches_model->submit_scores($id, $score1, $score2);
}

型号:

public function submit_scores($id, $score1, $score2) {
    if (empty($id) || empty($score1) || empty($score2)) { // <-- Here`s the culprit
        return 0;
    }

    $data = array(
        'partic1_score' => $score1,
        'partic2_score' => $score2
    );
    $this->db->where('id', $id);
    $this->db->update('matches', $data);
    return ($this->db->affected_rows() > 0) ? 1 : 0;
}

【问题讨论】:

  • s1和s2的值是多少?
  • id是匹配的id,s1s2input里面的值。
  • echo $id .', ' . $score1 . ', ' . $score2; exit;empty 条件之前的结果是什么?添加有问题的输出
  • 取决于input 标签的值(添加到问题中)。例如,我曾经通过console.log 转储响应进行测试;它返回id =&gt; "17"score1 =&gt; "2"score2 =&gt; "0"
  • 如果任何值是0,那么在 php empty 函数中返回 true。查看here了解更多信息

标签: javascript php jquery ajax codeigniter


【解决方案1】:

来自docs

在php中,如果有任何值作为0,那么empty函数返回为true

因此,如果您也想验证 0 值,请更改您的 if 条件,如下所示:

试试这个:

if ($id == '' || $score1 == '' || $score2 == '') {
    return 0;
}

【讨论】:

  • 好的,如果我使用empty(),这就解释了。我刚刚使用isset() 进行了测试,但即使我有或没有0,它也会返回true
  • phpisset() 函数检查变量是否设置,它总是返回true,因为所有变量总是通过ajax调用设置
  • 哎呀,我的意思是!isset()。对不起
  • 你的意思是if (!isset($id) || !isset($score1) || !isset($score2)) {?返回真?
  • 是的,基本上我想检查这些变量是否为空或未设置并返回 0 或 false。我只是想知道为什么我必须使用$var == NULL
【解决方案2】:

ajax 中的变量 id s1s2 只是空的。

在 php 中它们已设置,但为空

【讨论】:

  • id是匹配的id,s1s2input里面的值。我通过var_dump()确定它不是空的。
  • 如果 s1s2 中包含零 - 它被标记为空
  • 好的,如果我使用empty(),这就解释了。我刚刚使用isset() 进行了测试,但即使我有或没有0,它也会返回true
  • isset() 只检查变量是否存在尝试 isset($_POST["s3"]) 你会收到 false
  • 对不起,我的意思是!isset()
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-04
  • 1970-01-01
  • 2015-10-20
  • 2011-09-30
  • 1970-01-01
相关资源
最近更新 更多