【发布时间】: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,s1和s2是input里面的值。 -
echo $id .', ' . $score1 . ', ' . $score2; exit;在empty条件之前的结果是什么?添加有问题的输出 -
取决于
input标签的值(添加到问题中)。例如,我曾经通过console.log转储响应进行测试;它返回id => "17",score1 => "2",score2 => "0" -
如果任何值是
0,那么在 phpempty函数中返回 true。查看here了解更多信息
标签: javascript php jquery ajax codeigniter