php第二,如果验证不起作用

时间:2014-01-25 07:30:41

标签: php mysql

php第二,如果验证不起作用

第二个,如果是:$ firstnameErr =“只允许字母和空格”;没有直接工作重定向到success.php。

帮助我的朋友

<?php
    $firstname="";
    $firstnameErr="";

    if ($_SERVER['REQUEST_METHOD']== "POST") {
       $valid = true;

     if(empty($_POST["fname"]))
    {
        $firstnameErr="*firstname is Required";
        $valid=false;   
    }
    else
    {
    $firstname=test_input($POST["fname"]);
     if (!preg_match("/^[a-zA-Z ]*$/",$firstname))
           {
           $firstnameErr = "Only letters and white space allowed"; 
           }    
    } 

     //if valid then redirect
      if($valid){
          include 'database.php';
          echo '<META HTTP-EQUIV="Refresh" Content="0; URL=success.php">';    
        exit;
    //   header('Location: datasubmitted.php');
    //   exit();
      }  

    }

    function test_input($data)
    {
         $data = trim($data);
         $data = stripslashes($data);
         $data = htmlspecialchars($data);
         return $data;
    }

    ?>

1 个答案:

答案 0 :(得分:0)

当firstname有除字母和&amp;之外的额外字符时您需要将$ valid设置为false的空格。否则,无论它包含什么字符,它都会重定向到成功。

       $firstname=test_input($_POST["fname"]); //<-- changed this line. you have missed "_"
       if (!preg_match("/^[a-zA-Z ]*$/",$firstname))
       {
          $firstnameErr = "Only letters and white space allowed"; 
          $valid = false; //<-- Add this line
       }