简体   繁体   English

表单页面上的PHP表单错误消息

[英]PHP Form Error Message on Form Page

I am pretty new to PHP and I can't seem to find what I am looking for. 我对PHP还是很陌生,但似乎找不到我想要的东西。 I want the error to show on the page where the form is filled out. 我希望错误显示在填写表单的页面上。 I want a "Please enter name" error to show if it's blank in the $showerror part of the form page. 我希望在表单页面的$ showerror部分显示“请输入名称”错误。

This is what I have so far... 这是我到目前为止所拥有的...

<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>

Then this is what my process.php looks like... I am not sure how to code the error part. 这就是我的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!.";
    }
?>

You need to place PHP syntax between PHP tags, for example, this line: 您需要在PHP标记之间放置PHP语法,例如,以下行:

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

becomes like this: 变成这样:

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

If you're completely new to PHP, you can start learning from some tutorial websites, here's a good one 如果您完全不熟悉PHP,则可以从一些教程网站开始学习,这是一个不错的选择

EDIT: 编辑:

You can set $showerror in the PHP page, this could be a long list of "if conditions" for each form field, but I will show you a small/simple example for the full-name $_POST['name'] , it will be like this: 您可以在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';
}

compiler only parse content as php which is written between <?php ?> tag so use 编译器仅将内容解析为<?php ?>标记之间编写的php,因此使用

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

or 要么

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

Try this, 尝试这个,

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

You will get an error message Undefined variable if you do not have a code in validating the $showerror variable on the first compilation of the page 如果您没有在$showerror的第一次编译中验证$showerror变量的代码,则会收到错误消息Undefined variable

Try this.. you have to put both the code in same file. 试试这个..您必须将两个代码都放在同一个文件中。 and can check if the request is post the only read the php. 并可以检查请求是否发布仅读取php。 and then you can put the value in $sho 然后可以将值放入$ sho

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM