On one file I have this code:
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>
and on the receiving file I have this:
<?php echo $_POST["lqd_9"]; ?>
How can I make the code to not display anything if it's left empty?
Its a best practice to use isset and to escape characters like:
<?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
}
This simple check will guarantee that there is post data lqd_9 :)
ISSET checks if its set at first time else if you are using it without its set it may throw error. And the second check is checking if lqd_9 is different from FALSE (if its empty its false else its true)
<?= isset(($_POST['lqd_9']) ? $_POST['lqd_9'] : ''; ?>
That said, it's a bad practice to display user provided data without saniting it. You could create a small convenience function that returns sanitized data /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'];
}
?>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.