忽略所有受影响的文字 。
这场斗争是真实的。在主要是HTML代码的index.php
文件中,我有多个<span>
元素充当class="delete_this"
的按钮:
<ul id="products">
<li class="product">
<!-- more code -->
<div class="select-delete">
<span class="delete-product delete_this" title="Delete"></span>
</div>
</li>
<li class="product">
<!-- more code -->
<div class="select-delete">
<span class="delete-product delete_this" title="Delete"></span>
</div>
</li>
<li class="product">
<!-- more code -->
<div class="select-delete">
<span class="delete-product delete_this" title="Delete"></span>
</div>
</li>
</ul>
当点击任何带有<span>
的{{1}}元素(技术上任何)时,会发出jQuery AJAX请求:
class="delete_this"
// The exact following line is a simple on click function, it's a little different
// than the usual since I'm loading .delete_this also dynamically. It runs when you click
// on any element *.delete_this
$("#products").on("click", ".delete_this", function(event) {
var $count = $("#products > li").length;
$.ajax({
method: "POST",
url: "validate_existence.php",
// The following sends some data to the validate_existence.php file
data: {"quantity_of_products": $count},
success:function(html) {
if (html == "true") {
// condition met
} else {
// condition not met
}
}
因此,用户点击 });
});
,然后:(1)定义变量并且(2)请求AJAX,作为数据发送该变量的值。
现在,在<span class="delete_this">
中,我希望使用validate_existence.php
进行条件语句。要测试的标准是AJAX请求中发送的变量if () {}
是否等于1:
$count
问题是index.php中<?php
if ( $_POST['quantity_of_products'] = 1 ) { // Edit: thanks everyone for saying to use two equal symbols
// Run code if criteria is met
} else {
// Run code if criteria is not met
}
?>
内的代码始终运行,即使未满足条件(AKA // condition not met
)。< /秒>
我很可能在$_POST['quantity_of_products'] /= 1
文件中丢失了某些内容,或者在中错误地使用了validate_existence.php
function(html)
。
答案 0 :(得分:3)
更改以下行:
if($_POST['quantity_of_products'] = 1) // This is assignment
到
if($_POST['quantity_of_products'] == 1) // This is condition checking
答案 1 :(得分:2)
$_POST['quantity_of_products'] = 1
正在为$_POST['quantity_of_products']
变量赋值。所以你必须像这样使用$_POST['quantity_of_products']==1
if($_POST['quantity_of_products'] == 1)
答案 2 :(得分:0)
我认为最好的方法是使用三元运算符:
<?php
if ( $_POST['quantity_of_products'] = 1 ) {
echo true;
} else {
echo false;
}
?>
用
替换代码<?php
$test = $_POST['quantity_of_products'] == 1? true: false;
echo $test;