[英]PHP Form Error Message on Form Page
我对PHP还是很陌生,但似乎找不到我想要的东西。 我希望错误显示在填写表单的页面上。 我希望在表单页面的$ showerror部分显示“请输入名称”错误。
这是我到目前为止所拥有的...
<form method="post" action="process.php">
<table width="667">
<td colspan="2"><?php echo $showerror; ?></td>
<tr>
<td>Full Name:*</td>
<td><input class="text" name="name" placeholder="Ex. John Smith"></td>
<tr>
<td>E-mail:*</td>
<td><input class="text" name="email" placeholder="Ex. johnsmith@gmail.com"></td>
<tr>
<td>Type of site:*</td>
<td>Business<input name="multioption[]" type="radio" value="Business" class="check" /> Portfolio<input name="multioption[]" type="radio" value="Portfolio" class="check" /> Forum<input name="multioption[]" type="radio" value="Forum" class="check" /> Blog<input name="multioption[]" type="radio" value="Blog" class="check" /></td>
<tr>
<td>Anti-Spam: (2)+2=?*</td>
<td><input name="human" placeholder="What is it?"></td>
</table>
<input id="submit" name="submit" type="submit" value="Submit"></form>
这就是我的process.php的样子...我不确定如何编码错误部分。
<?php
$name = isset($_POST['name']) ? $_POST['name'] : '';
$email = isset($_POST['email']) ? $_POST['email'] : '';
$human = isset($_POST['human']) ? $_POST['human'] : '';
$submit = isset($_POST['submit']) ? true : false;
$multioption = isset($_POST['multioption'])
? implode(', ', $_POST['multioption'])
: 'No multioption option selected.';
$from = 'From: Testing Form';
$to = 'xx@xxxxx.com';
$subject = 'Testing Form';
$body =
"Name: $name\n
E-Mail: $email\n
Multi Options: $multioption\n";
if ($submit && $human == '4') {
mail ($to, $subject, $body, $from);
print ("Thank you. We have received your inquiry.");
}
else {
echo "We have detected you are a robot!.";
}
?>
您需要在PHP标记之间放置PHP语法,例如,以下行:
<td colspan="2">$showerror</td>
变成这样:
<td colspan="2"><?php echo $showerror; ?></td>
如果您完全不熟悉PHP,则可以从一些教程网站开始学习,这是一个不错的选择
编辑:
您可以在PHP页面中设置$ showerror,这可能是每个表单字段的一长串“如果条件”,但是我将向您展示一个小/简单的全名$_POST['name']
示例,它将是这样的:
$showerror = '';
if(!empty($_POST['name'])) {// enter here if fullname is set
if(strlen($_POST['name']) >= 6 && strlen($_POST['name']) <= 12) {// enter here if fullname length is between 6-12 characters
// You can do more validation by using "if conditions" here if you would like, or keep it empty if you think fullname is correct
} else {// enter here if fullname is NOT between 6-12 characters
$showerror = 'Full name must be 6-12 characters';
}
} else {// enter here if fullname is not set
$showerror = 'Please enter your full name';
}
编译器仅将内容解析为<?php ?>
标记之间编写的php,因此使用
<td colspan="2"><?php echo $showerror; ?></td>
要么
<td colspan="2"><?= $showerror ?></td>
尝试这个,
<?php
if(isset($showerror))
echo $showerror;
else
echo '';
?>
如果您没有在$showerror
的第一次编译中验证$showerror
变量的代码,则会收到错误消息Undefined variable
试试这个..您必须将两个代码都放在同一个文件中。 并可以检查请求是否发布仅读取php。 然后可以将值放入$ sho
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.