表单上的必填字段显示错误消息 [英] Required field display error message on form

查看:57
本文介绍了表单上的必填字段显示错误消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表格,需要填写必填字段.我知道使用下面的代码来验证该字段是否为空:

I have a form that I need to have required fields filled out. I know to use the code below to verify if the field is blank:

<?php
if  (!empty($_POST['client_name'])) {
echo '<p style="color:red;">'"Client Name is required!"'</p>';
}
?>

我的问题是,如何让错误消息显示在表单页面上,同时保存已在表单中输入的所有数据.示例:我填写了表单上的所有 15 个字段,不包括必填字段.当我点击提交按钮时,如果必填字段为空,我想留在该表单页面上,而不会丢失我放入字段中的任何信息,并且我想在必填字段框旁边显示一条消息,说这是必填字段.

My question is, how do I get the error message to display on the form page, saving all the data already entered in the form. Example: I fill out all 15 fields on the form, excluding the required field. When I hit the submit button, if the required field is empty, I want to stay on that form page, without losing any of the info I put into the fields, and I want to display a message next to the required field box, saying "This is a required field.

我不确定要执行此操作的代码,或将其放在哪里.在表单上,​​还是在执行表单的脚本上?

I am not sure on the code to do that, or where to put it. On the form, or on the script that executes the form?

推荐答案

先使用客户端 javascript 验证,然后使用 php 服务器端验证.

use client side javascript validation first, then php server side validation.

这篇关于表单上的必填字段显示错误消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆