用php验证表单

用php验证表单,php,validation,Php,Validation,我有一个php表单,我需要验证表单。所以我尝试了这些代码,但没有得到任何验证消息。没有显示任何内容。我得到的错误是什么 <form class="" method="post" action=""> <div class ="col-md-3" style="clear:both;"> <label style="font-size:12px;"> Your Name:</label> <inp

我有一个php表单,我需要验证表单。所以我尝试了这些代码,但没有得到任何验证消息。没有显示任何内容。我得到的错误是什么

<form class="" method="post" action="">
<div class ="col-md-3" style="clear:both;">
            <label style="font-size:12px;"> Your Name:</label>
            <input type="text" id="AS_Name" name="AS_Name" />
            <span style="font-size:12px; color:red;"> <?php echo $nameError; ?> </span>
            </div>
    <input  id="AstroSend" type="submit" Value="Send" name="submit" />
   </form>

<?php
 $nameError ="";
if(isset($_POST['submit'])) {

if (empty($_POST["AS_Name"]))
    {   

    $nameError = "Name is required";

    } 
else 
    {

    $name = test_input($_POST["AS_Name"]);
    // check name only contains letters and whitespace
    if (!preg_match("/^[a-zA-Z ]*$/",$name)) 
    {
    $nameError = "Only letters and white space allowed";
    }
    } ?>

你的名字:

如果我在
If
循环中回显,则它将显示消息。但是我希望验证消息在
span
本身中。

您只需要检查html上方而不是下方的验证:)

进行以下更改

<?php
 $nameError ="";
 if(isset($_POST['submit'])) 
 {
   if (empty($_POST["AS_Name"]))
   {   
    $nameError = "Name is required";
   }

  else 
 {

  $name = test_input($_POST["AS_Name"]);
// check name only contains letters and whitespace
     if (!preg_match("/^[a-zA-Z ]*$/",$name)) 
     {
       $nameError = "Only letters and white space allowed";
     }
   } 
  }
?>

<form class="" method="post" action="">
<div class ="col-md-3" style="clear:both;">
        <label style="font-size:12px;"> Your Name:</label>
        <input type="text" id="AS_Name" name="AS_Name" />
        <span style="font-size:12px; color:red;"> <?php echo $nameError;?> </span>
          </div>
       <input  id="AstroSend" type="submit" Value="Send" name="submit" />
       </form>

你的名字:

我不是那个否决投票的人,但在你的文章中,其他人被放错了@coder

我工作得很好

新手可以检查一下吗

<?php
$nameError ="";
if(isset($_POST['submit'])) 
} 其他的 { $name=$\u POST[“AS\u name”]

{

}

}

?>
你的名字:

您是否尝试过$\u POST查看表单是否已发布是的,我可以输入
if
循环,我可以回显消息,将php代码移到top我尝试过将php移到top。但当我单击“提交”时,表单将消失。您在
if(isset($\u POST['submit']){
之后打开一个括号,但您从未关闭它。了解如何格式化代码。只需按您的需要使用制表器,2秒钟后您就会看到此错误。
if (empty($_POST["AS_Name"]))
{   
$nameError = "Name is required";
if (!preg_match("/^[a-zA-Z ]*$/",$name)) 
$nameError = "Only letters and white space allowed";
} 
?>
<form class="" method="post" action="">

<div class ="col-md-3" style="clear:both;">
            <label style="font-size:12px;"> Your Name:</label>
        <input type="text" id="AS_Name" name="AS_Name" />
        <span style="font-size:12px; color:red;"> <?php echo $nameError;?> </span>

</div>
<input  id="AstroSend" type="submit" Value="Send" name="submit" />
   </form>