当$order_status
接受时,仅禁用包含接受的按钮。如果$order_status
Dispatched ,则会发送已禁用的按钮。如果$order_status
已取消,则已禁用按钮为取消。我是php的新手,我不会安静地理解它。
包含文字接受的按钮已被停用 - 即使$order_status
的值已接受,已发送或取消。为什么总是禁用?
下面是代码:
<button type="submit" name="submitAccept" class="btn btn-primary"
<?php if ($order_status = 'Accepted'){?> disabled <?php } ?>">Accept </button>
<button type="submit" name="submitSent" class="btn btn-primary" >Sent</button>
<button type="submit" name="submitCancel" class="btn btn-danger"
onclick="if(!confirm('Are you sure you want to cancel order?')){return false;}">Cancel</button>
答案 0 :(得分:0)
将您的疑虑分开是一个好习惯。首先从你的HTML和尽可能少的PHP开始:
<button
type="submit"
name="submitAccept"
class="btn btn-primary"
<?php if ($submitAcceptDisabled): ?> disabled <?php endif ?>">
Accept
</button>
您的其他按钮也是如此,只需添加一个标记并保持您的视图简单。对于PHP逻辑,让我们调查您的场景:
当order_status被接受时,仅禁用接受按钮。
$submitAcceptDisabled = $order_status === 'Accepted';
如果发送了order_status,则会发送禁用按钮。
$submitSentDisabled = $order_status === 'Dispatched';
如果order_status为Cancelled,则禁用按钮为Cancel。
$submitCancelDisabled = $order_status === 'Cancelled';
在您的代码中,您为$order_status
分配一个值,而不是检查当前值是否等于:
if ($order_status = 'Accepted')
这将始终将Accepted
的值设置为$order_status
变量。
您可以考虑的另一项改进是将您的Javascript分开:
const submitCancel = document.querySelector('button[name="submitCancel"]');
submitCancel.addEventListener('click', () => {
if(!confirm('Are you sure you want to cancel order?')) {
return false;
}
});
答案 1 :(得分:0)
条件语句中的单独等号不是“Comparison Operator”,而是“Assignment Operator”。
如果你写:
$order_status='Dispatched';
if($order_status='Accepted'){ // this sets the value
echo "order_status=$order_status"; // this displays the new value
}else{
echo "order_status=$order_status"; // this doesn't get a chance to happen
}
你会看到:
order_status=Accepted
这是因为if语句的行为类似于声明而不是比较。要使if语句将变量与值进行比较,可以使用==
(松散比较,在“类型杂耍”之后也称为真)或===
(严格比较,又称“相同”)。
对于您的任务,您可以使用在单个php代码块中使用内联条件的此代码段。
代码:(Demo)
echo "<button type=\"submit\" name=\"submitAccept\" class=\"btn btn-primary\"",($order_status=='Accepted'?' disabled':''),">Accept<button>";
echo "\n";
echo "<button type=\"submit\" name=\"submitSend\" class=\"btn btn-primary\"",($order_status=='Dispatched'?' disabled':''),">Send<button>";
echo "\n";
echo "<button type=\"submit\" name=\"submitCancel\" class=\"btn btn-danger\"",($order_status=='Cancelled'?' disabled':'')," onclick=\"if(!confirm('Are you sure you want to cancel order?')){return false;}\">Cancel<button>";
未呈现的HTML输出:
<button type="submit" name="submitAccept" class="btn btn-primary" disabled>Accept<button>
<button type="submit" name="submitSend" class="btn btn-primary">Send<button>
<button type="submit" name="submitCancel" class="btn btn-danger" onclick="if(!confirm('Are you sure you want to cancel order?')){return false;}">Cancel<button>
一些额外的说明:
"
后?>
额外?>">Accept </button>
:Sent
Send
更改为let text = "0.91% ABC DEF"
以提高英语水平