[英]if value is empty display nothing
在一個文件中,我有以下代碼:
if (theForm.lqd_9.value == "")
{
alert("You have forgotten to specify - Description!");
theForm.lqd_9.focus();
return (false);
<tr>
<td class="theadingt" align="center" height="14" width="180">
<b>Description:<span lang="en-us"><font color="#ff0000">*</font></span></b></td>
<td class="theading" align="left" height="14" width="545">
<textarea cols='48' rows='6' name="lqd_9"></textarea></td>
</tr>
在接收文件上我有這個:
<?php echo $_POST["lqd_9"]; ?>
如果留空,如何使代碼不顯示任何內容?
<?php
if(isset($_POST["lqd_9"])){
echo $_POST["lqd_9"];
}
?>
<?php
if(!empty($_POST["lqd_9"])){
echo $_POST["lqd_9"];
}
?>
最好同時使用以下兩種方法。
<?php
if(isset($_POST["lqd_9"]) && !empty($_POST["lqd_9"])){
echo $_POST["lqd_9"];
}
?>
最佳做法是使用isset並轉義以下字符:
<?php
if(isset($_POST["lqd_9"]) && trim($_POST["lqd_9"]) !== ""){
echo htmlspecialchars($_POST["lqd_9"], ENT_COMPAT | ENT_HTML401, 'UTF-8');
}
?>
if(isset($_POST["lqd_9"]) && $_POST["lqd_9"]){
###ALL CODE
}
這個簡單的檢查將確保有發布數據lqd_9 :)
ISSET會在第一次檢查它的設置,否則,如果您使用的是它沒有設置的設置,則可能會拋出錯誤。 第二個檢查是檢查lqd_9是否與FALSE不同(如果為空則為false,否則為true)
<?= isset(($_POST['lqd_9']) ? $_POST['lqd_9'] : ''; ?>
就是說,在不清理用戶提供的數據的情況下,這是一個錯誤的做法。 您可以創建一個小的便利函數,該函數返回已清理的數據/ ex:
<?php
function sanitize($str) {
return filter_var($str, FILTER_SANITIZE_STRING);
}
?>
<?= isset($_POST['lqd_9']) ? sanitize($_POST['lqd_9']) : '' ?>
<?php
if(isset($_POST['lqd_9']){
echo $_POST['lqd_9'];
}
?>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.