使用PHP验证HTML页面上的字段

8 浏览
0 Comments

使用PHP验证HTML页面上的字段

请找到index.html和submit.php文件的代码。在loopo的帮助下,两个文件都完美运行。我在哪里放置验证代码(在index.html文件还是submit.php文件中)感到困惑。我在html文件中使用了html5的输入类型,但不知道验证应该在submit.php文件的哪个位置进行。我有多个表单,并按照建议创建了validations.php文件。我也不明白错误消息将显示在哪里?

你能建议我在submit.php文件中的哪个位置通过编辑我的submit.php文件来添加这些验证吗?

邮箱的正则表达式('/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/'
电话的正则表达式(^(?:(?:\+|0{0,2})91(\s*[\-]\s*)?|[0]?)?[789]\d{9}$

index.html文件

submit.php文件

escape_string($_POST['name']);
   $city = $con->escape_string($_POST['city']);
   $email = $con->escape_string($_POST['email']);
   $mobile = $con->escape_string($_POST['mobile']);
   $sub = $con->escape_string($_POST['sub']);
   $slogan = $con->escape_string($_POST['slogan']);
   $sql="INSERT INTO members 
            (sName, sCity, sMobile, sEmail, sSub, sSlogan)
         VALUES ('$name', '$city', '$mobile', '$email',
                '$sub','$slogan')";
   if (!$con->query($sql)){ //之前忘记了一个括号
      $output .= '错误:数据库返回:('.$con->errno.')'.$con->error.$nl;
      $output .= '查询语句:'.$sql.$nl;
      $errors = true;
   }else{
     $output .= "添加了1条记录".$nl;
   }
}
if (!$errors){
   //如果没有错误,则重定向到index.html;
   header('refresh: 2; URL='.$redirect_url);
   $output .= '...重定向中...'.$nl;
}else{
   //显示错误,并允许显示返回/重新尝试的链接
   $output .= '重新尝试'.$nl;
}
echo $output;
?>

loopo建议的验证,但不知道放置的确切位置。我创建了一个validations.php文件并在submit.php中包含了它,但可能是因为语法错误,所以它不起作用。

function validate_name($input){
    //相当简单的规则:
    //大写和小写的拉丁字符和空格
    //至少三个字符长
    //您可能希望考虑允许其他字符,如é ö等。
    $input = trim($input); //去掉两端的空格
    if (preg_match('/^[a-zA-Z ]{3,}$/',$input) == 1){
        return $input;
    }else{
        return false;
    }
}
  if (!empty($_POST['name']){
    if (!$name = $con->escape_string(validate_name($_POST['name'])){
        $error = true;
        $output .= '错误:无效的姓名:'.$_POST['name'].$nl;
    }
  }else{
    $error = true;
    $output .= '错误:未指定姓名'.$nl;
  }

0
0 Comments

在HTML页面上进行验证的原因是因为可以通过直接发送请求到php文件来绕过HTML验证,所以在服务器端进行验证是一个明智的选择。建议使用php的内置函数filter_var进行验证,以便获得干净的代码和准确/安全的结果。可以尝试以下代码:

function validateEmail($email){
    if(filter_var($email,FILTER_VALIDATE_EMAIL)){
        return true;
    }
}
function validatePhonenb($nb){
    if(filter_var($nb,FILTER_VALIDATE_REGEXP,array("options"=>array("regexp"=>"/^(?:(?:\+|0{0,2})91(\s*[\-]\s*)?|[0]?)?[789]\d{9}$/")))){
        return true;
    }
}

由于没有对电话号码进行验证,所以使用了您的正则表达式。此外,您可以在继续之前使用var_filter对电话号码进行清理。

I have multiple forms and I made validations.php as suggested. I am also not understanding where will the error messages been shown

错误消息将显示在页面底部,因为它们在代码末尾被回显:

echo $output;
?>

如果它们没有显示,请改变回显包含错误的输出的位置,并在此之后执行die(),这样脚本在显示错误后就停止执行了。

Thanks. Where do i put this code, in submit.php or validations.php. If submit.php, at what location should I put it?

您应该具备处理php的知识,无论如何,这段代码必须放在validations.php中,并从submit.php中调用。

I agree... I am practicing php.. can you please give me the syntax to call the function from submit.php? Thanks

如果(validateEmail("email.com")) { code here } if(validatePhonenb("0011234567890")){ code here }

Thanks. I also understood the issue with the code why it was not working. I was missing parenthesis in the if (!empty($_POST['name']){ line and the following line. Also instead of $error = true; it should have been $errors = true; missed the letter s. I want to ask one thing. When the errors are displayed in case it is not validated, it gives a link to try again but the data entered goes off. How can I get it back?

我相信您正在寻找autocomplete属性,这个属性必须添加到您正在提交的表单中,然后您的form标签必须像这样:

Well not really... If you see the line      $output .= 'Try again'.$nl;     in submit.php file, it redirects to the url and I want the redirection to be the BACK function of browser so that the data entered stays there.

如果您看到submit.php文件中的这一行:$output .= 'Try again'.$nl;,它会重定向到URL,我希望重定向到浏览器的返回功能,以便保留输入的数据。

0