drjtua5953
drjtua5953
2013-05-27 04:07

如果值为空则不显示任何内容

已采纳

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?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

5条回答

  • dsnhalq37505 dsnhalq37505 8年前

    You could use methods like isset or empty

    <?php 
    if(isset($_POST["lqd_9"])){
     echo $_POST["lqd_9"];
    } 
    ?>
    

    OR

    <?php 
    if(!empty($_POST["lqd_9"])){
     echo $_POST["lqd_9"];
    } 
    ?>
    

    update

    It's better to use both as below.

    <?php 
    if(isset($_POST["lqd_9"]) && !empty($_POST["lqd_9"])){
     echo $_POST["lqd_9"];
    } 
    ?>
    

    PHP isset() vs empty() vs is_null()

    点赞 评论 复制链接分享
  • duanbohan2015 duanbohan2015 8年前
    <?= 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']) : '' ?>
    
    点赞 评论 复制链接分享
  • dongyashun2559 dongyashun2559 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)

    点赞 评论 复制链接分享
  • doupo6967 doupo6967 8年前

    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');
        }
    ?>
    
    点赞 评论 复制链接分享
  • dpca31461 dpca31461 8年前
    <?php 
       if(isset($_POST['lqd_9']){
           echo $_POST['lqd_9'];
       }
    ?>
    
    点赞 评论 复制链接分享

为你推荐