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 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!