dousuohe5882 2014-05-17 13:32
浏览 24
已采纳

如何显示空字段错误消息

I have a page which allows the user to "create a topic", open submitting this the form goes to another through a verification process which inserts the topic into the database and re-directs to back to the main page. However I want my verification page "add topic" to display an error message if all fields are not filled in. here is a my code, please can you tell me where I would need to add this validation code to notify the user to fill all fields:

// get data that sent from form 
$topic=$_POST['topic'];
$detail=$_POST['detail'];
$name=$_POST['name'];
$email=$_POST['email'];

$datetime=date("d/m/y h:i:s"); //create date time





$sql="INSERT INTO $tbl_name(topic, detail, name, email, datetime)VALUES('$topic', '$detail', '$name', '$email', '$datetime')";
$result=mysql_query($sql);




if($result){
echo "Successful<BR>";
echo "<a href=main_forum.php>View your topic</a>";
}
else {

echo "ERROR";
}
mysql_close();
  • 写回答

2条回答 默认 最新

  • douchuang1861 2014-05-17 13:57
    关注

    My suggestion would be create a separate php file called validation and inside the validation file add a function. Of course you can create this function inside the same php file. If you made the separate use an include statement to place it on your page. Also a quick post-back to itself would be good since you could easily be able to get access to the posted variables and already be on the page to show errors. Otherwise you would have to return the Errors in a get, post or session. If everything was successful you could post or redirect right after the postback (maybe to a success page) and the user would only see the postback if errors present.

    include_once("Validation.php"); 
    

    as shown above.

    validateNewTopic($topic, $detail, $name, $email, $datetime)
    {
    
    
    }
    

    Then inside you could use if statements to check conditions. If you want a quick solution you can create a variable to hold all the errors.

        $Error = "<p class='errors'">;
    
        if ($topic == "")
        {
         $Error+="topic is required";
        }
    
    
        if ($Error != "<p class='errors'">)
        {
    
            return $Error +"</p>";
    
        }
    else
    {
    return "";
    }
    

    Since you are posting the values you can catch them in a variable on postback to validate.

    $topic = $POST['topic'];
    
    $Error=validateNewTopic($topic);
    
    if ($Error != "")
    {
      ?>
    echo $Error
    <?php
    }
    else {
    //run sql code and show success
    }
    

    By putting the paragraph tags inside the $Error messages we can just echo and it will already be in the paragraph tag with the class errors. You can make it prettier by using an un-ordered list and when adding an error using list items. I'm not sure how familiar you are with php but at anytime you can stop writing php code by closing the tags. (< php ?> and reopen < ? php) as shown above in the if statement. I know this was not 100% clear but this is something you should try/research and practice since it is used so often. Good luck!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀