需要使用 php 的表单字段
鉴于以下代码示例,我需要向 check_input 函数添加什么,以便它处理丢失/必需的表单字段。基本上,我想做的就是在最终用户尝试提交表单而不填写所有必填字段时,在表单顶部向他们显示一条错误消息,其中显示类似“标有 * 的字段是必需的”之类的内容。
如有任何帮助,我们将不胜感激,并提前感谢您的宝贵时间。
// Don't post the form until the submit button is pressed.
if(isset($_POST['submit'])) {
echo(
check_input($_POST['name']) . <br> .
check_input($_POST['city']);
}
// check_input function
function check_input($data)
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data, ENT_QUOTES);
return $data;
}
表格
<form action="test.php" method="post">
<input type="text" name="name">
<input type="text" name="city">
<input type="submit" name="submit" value="submit">
</form>
Given the following code example, what would I need to add to the check_input function so that it deals with missing / required form fields. Basically, all I am trying to do is to show the end user an error message on the top of my form that says something like "Fields marked with a * are required" if they try to submit the form without filling out all the required fields.
Any help would be greatly appreciated and thank in advance for your time.
// Don't post the form until the submit button is pressed.
if(isset($_POST['submit'])) {
echo(
check_input($_POST['name']) . <br> .
check_input($_POST['city']);
}
// check_input function
function check_input($data)
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data, ENT_QUOTES);
return $data;
}
The Form
<form action="test.php" method="post">
<input type="text" name="name">
<input type="text" name="city">
<input type="submit" name="submit" value="submit">
</form>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
PS:我注意到您的 HTML 表单中存在引用不匹配的情况。该方法应为
method="post"
,而不是method="post'
。PS: I noticed a quote mismatch in your form HTML. The method should read
method="post"
, notmethod="post'
.