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 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵