【问题标题】:Prevent submitting if input field is empty如果输入字段为空,则阻止提交
【发布时间】:2017-11-18 20:02:27
【问题描述】:

我为我的社交媒体开发了一个评论系统,但是即使数据库是空的,cmets 也会插入到数据库中。所以如果评论字段为空,我不希望任何事情发生,我不想回显任何消息或提交评论。

这是我的代码

    <?php 
    // Get id of post
    if(isset($_GET['post_id'])) {

        $post_id = $_GET['post_id'];
    }

    $user_query = mysqli_query($con, "SELECT added_by, user_to FROM posts 
    WHERE id='post_id'");
    $row = mysqli_fetch_array($user_query);

    $posted_to = $row['added_by'];
    $user_to = $row['user_to'];

    if(isset($_POST['postComment' . $post_id])) {

        $post_body = $_POST['post_body'];
        $post_body = mysqli_escape_string($con, $post_body);
        $date_time_now = date("Y-m-d H:i:s");
    $insert_post = mysqli_query($con, "INSERT INTO comments VALUES ('', 
    '$post_body', '$userLoggedIn', '$posted_to', '$date_time_now', 'no', 
    '$post_id')");

    if($posted_to != $userLoggedIn) {

        $notification = new Notification($con, $userLoggedIn);
        $notification->insertNotification($post_id, $posted_to, "comment");
    }

    if($user_to != 'none' && $user_to != $userLoggedIn) {

        $notification = new Notification($con, $userLoggedIn);
        $notification->insertNotification($post_id, $user_to, 
        "profile_comment");
    }

    $get_commenters = mysqli_query($con, "SELECT * FROM comments WHERE 
    post_id='$post_id'");
    $notified_users = array();
    while($row = mysqli_fetch_array($get_commenters)) {

        if($row['posted_by'] != $posted_to && $row['posted_by'] != $user_to
            && $row['posted_by'] != $userLoggedIn && 
        !in_array($row['posted_by'], $notified_users)) {

            $notification = new Notification($con, $userLoggedIn);
            $notification->insertNotification($post_id, $row['posted_by'], 
           "comment_non_owner");

            array_push($notified_users, $row['posted_by']);

        }

    }



    echo "<p>Comment Posted! </p>";
    }

    ?>

   <form action="comment_frame.php?post_id=<?php echo $post_id; ?>" 
   id="comment_form" name="postComment<?php echo $post_id; ?>" 
    method="POST">
    <textarea name="post_body" placeholder="Add a comment"></textarea>
    <input type="submit" name="postComment<?php echo $post_id; ?>" 
     value="Comment"> 

 </form>

 <!-- Load Comments -->
 <?php 
 $get_comments = mysqli_query($con, "SELECT * FROM comments WHERE 
 post_id='$post_id' ORDER BY id ASC");
 $count = mysqli_num_rows($get_comments);

 if ($count != 0) {

    while($comment = mysqli_fetch_array($get_comments)) {

        $comment_body = $comment['post_body'];
        $posted_to = $comment['posted_to'];
        $posted_by = $comment['posted_by'];
        $date_added = $comment['date_added'];
        $removed = $comment['removed'];

        //Timeframe

        $date_time_now = date("Y-m-d H:i:s");
        $start_date = new DateTime($date_added); // Time of Post
        $end_date = new DateTime($date_time_now); // Current time
        $interval = $start_date->diff($end_date); // Difference between 
        dates
        if($interval->y >= 1) {
            if($interval == 1)
                $time_message = $interval->y . " year ago"; // 1 year ago
            else
                $time_message = $interval->y . " years ago"; // 1+ year ago
        }
        else if ($interval-> m >= 1) {

            if($interval->d == 0) {
                $days = " ago";
            }
            else if($interval->d == 1) {
                $days = $interval->d . " days ago";
            }
            else {
                $days = $interval->d . " days ago";
            }

             if($interval->m == 1) {
                $time_message = $interval->m . " month". $days;
             }
             else {

                $time_message = $interval->m . " months". $days;
             }
        }
        else if($interval->d  >=1) {
            if($interval->d == 1) {
                $time_message = "Yesterday";
            }
            else {
                $time_message = $interval->d . " days ago";
            }
        }
        else if($interval->h >= 1) {
            if($interval->h == 1) {
                $time_message = $interval->h . " hour ago";
            }
            else {
                $time_message = $interval->h . " hours ago";
            }
        }
        else if($interval->i >= 1) {
            if($interval->i == 1) {
                $time_message = $interval->i . " minute ago";
            }
            else {
                $time_message = $interval->i . " minutes ago";
            }
        }
        else {
            if($interval->s < 30) {
                $time_message = "Just now";
            }
            else {
                $time_message = $interval->s . " seconds ago";
            }
        }

        $user_obj = new User($con, $posted_by);
        ?>
        <div class="comment_section">
        <a href="<?php echo $posted_by?>" target="_parent"><img src="<?php 
         echo $user_obj->getProfilePic(); ?>" title="<?php echo $posted_by; 
         ?>" style="float:left;" height="30"></a>
        <a href="<?php echo $posted_by?>" target="_parent"> <b><?php echo 
        $user_obj->getFirstAndLastName(); ?> </b> </a>
        &nbsp;&nbsp;&nbsp;&nbsp; <?php echo $time_message . "<br>" . 
        $comment_body; ?>
        <hr>
        </div>
        <?php

     } 
     }

    else {

    echo "<center><br><br>No comments to show</center>";
     }

    ?>



  </body>
  </html>

【问题讨论】:

  • 在插入数据库之前检查评论字段的值是否为空。如果存在则插入,否则什么也不做。
  • 考虑到您已经在整个代码中进行条件检查...您只需要编写一个 if 语句来检查该输入是否为空。如果没有,请继续。如果是这样,请不要继续。说实话,我很惊讶你写了这段代码,但如果输入为空,仍然不得不询问是否让表单提交。
  • 您也可以使用 textarea w3c.github.io/html/sec-forms.html#the-required-attribute 上所需的属性,在理解的地方表格在填写之前不应发送和处理:)

标签: javascript php html css comments


【解决方案1】:

将此属性添加到正确的输入字段:&lt;input required: required;&gt; 应该会为您修复它:-)

更多信息请参见http://w3c.github.io/html/sec-forms.html#the-required-attribute

【讨论】:

  • 我已经评论过了,不过,您应该在清理检索到的数据后在服务器端处理此检查...
  • 我也会这样做,以免混淆任何人:D
【解决方案2】:

在下面包含一个演示。第一个表单在提交时会显示一条错误消息 - 属性 required - (您可能不想要),而第二个表单不提供任何反馈 - 属性 required 但表单的 novalidate

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form>
   <textarea required onsubmit="return false;"></textarea><br>
   <input type="submit" value="submit">
</form><br><br>

<form novalidate onsubmit="return false;">
   <textarea required></textarea><br>
   <input type="submit" value="submit">
</form>

【讨论】:

  • 正如对另一个答案的评论,该属性本身是不够的,并且可能无法在每个地方都被理解。您应该将资源链接到您的答案以使其值得;)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-10-28
  • 1970-01-01
  • 1970-01-01
  • 2017-10-23
  • 1970-01-01
  • 2012-03-11
  • 1970-01-01
相关资源
最近更新 更多