在下面的代碼中,如果我回顯變數,條件將起作用,如果我不回顯變數,條件將不起作用!
錯誤是什么?
編碼:
$msg=$_SESSION['$msg'];
echo $msg;
if($msg != null){ ?>
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@9"></script>
<script >
swal.fire({
icon: "success",
title: "success",
showConfirmButton: false,
timer: 1300
})
</script>
<?php } ?>
編輯過的代碼:除非回聲,否則即使這樣也不起作用!
$msg="ss";
if(!empty($msg)){ ?>
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@9"></script>
<script >
swal.fire({
icon: "success",
title: "success",
showConfirmButton: false,
timer: 1300
})
</script>
<?php } ?>
uj5u.com熱心網友回復:
代碼有幾個問題:
session_start();在使用 $_SESSION 之前呼叫。$_SESSION['$msg']可以是未定義的并且可以觸發通知。您應該檢查密鑰是否存在isset($_SESSION['$msg'])。會話密鑰
$msg有點奇怪。您不需要$for 會話密鑰。如果要檢查 not
null,請使用嚴格比較!==。
<?php
session_start();
$msg = isset($_SESSION['$msg']) ? $_SESSION['$msg'] : null;
echo $msg;
if($msg !== null){ ?>
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@9"></script>
<script >
swal.fire({
icon: "success",
title: "success",
showConfirmButton: false,
timer: 1300
})
</script>
<?php } ?>
uj5u.com熱心網友回復:
Firstly, in line number 1 you should pull the session key, so if its a variable it should be
$msg=$_SESSION[$msg];
else it should be the key name
$msg=$_SESSION['msg']; //msg can be replaced by your key name.
You can check directly whether the key exist or not and if it is empty as below:
if(isset($_SESSION['msg']) && $_SESSION['msg'] != ''){ ?>
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@9"></script>
<script >
swal.fire({
icon: "success",
title: "success",
showConfirmButton: false,
timer: 1300
})
</script>
<?php } ?>
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/370103.html
標籤:javascript php
