我是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!.";
}
?>
答案 0 :(得分:2)
您需要在PHP标记之间放置PHP语法,例如,此行:
<td colspan="2">$showerror</td>
变成这样:
<td colspan="2"><?php echo $showerror; ?></td>
如果您对PHP完全陌生,可以从一些教程网站开始学习,这里是good one
修改强>
您可以在PHP页面中设置$ showerror,这可能是每个表单字段的“if conditions”的长列表,但我将向您展示全名$_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';
}
答案 1 :(得分:2)
编译器只解析内容为php,它写在<?php ?>
标记之间,所以使用
<td colspan="2"><?php echo $showerror; ?></td>
或
<td colspan="2"><?= $showerror ?></td>
答案 2 :(得分:0)
试试这个,
<?php
if(isset($showerror))
echo $showerror;
else
echo '';
?>
如果您在第一次编辑页面时没有验证Undefined variable
变量的代码,则会收到错误消息$showerror
答案 3 :(得分:0)
试试这个..你必须将两个代码都放在同一个文件中。 并且可以检查请求是否发布只读了php。 然后你可以将值放在$ sho
中