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 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 MATLAB中streamslice问题
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 51单片机中C语言怎么做到下面类似的功能的函数(相关搜索:c语言)
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序