0

我对 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!.";
    }
?>
4

4 回答 4

2

您需要在 PHP 标记之间放置 PHP 语法,例如,这一行:

<td colspan="2">$showerror</td>

变成这样:

<td colspan="2"><?php echo $showerror; ?></td>

如果你完全是 PHP 新手,你可以从一些教程网站开始学习,这里有一个很好的

编辑:

您可以在 PHP 页面中设置 $showerror,这可能是每个表单字段的“if 条件”的长列表,但我将向您展示 full-name 的一个小/简单示例$_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';
}
于 2013-06-24T09:15:17.810 回答
2

编译器仅将内容解析为写在<?php ?>标签之间的 php,因此请使用

<td colspan="2"><?php echo $showerror; ?></td>

或者

<td colspan="2"><?= $showerror ?></td>
于 2013-06-24T09:18:01.377 回答
0

试试这个,

 <?php
    if(isset($showerror))
       echo $showerror;
    else 
       echo '';
  ?>

Undefined variable如果您在第一次编译页面时没有验证$showerror变量的代码,您将收到一条错误消息

于 2013-06-24T09:26:56.293 回答
0

试试这个..你必须把两个代码放在同一个文件中。并且可以检查请求是否是发布的唯一读取的 php.ini 文件。然后您可以将值放入 $sho

于 2013-06-24T09:31:23.890 回答