为什么变量仍然通过(名称验证)? * BEGINNER *

问题描述 投票:0回答:1

我对PHP相当陌生,在验证输入时遇到了麻烦。出现消息,但是,变量(应该被验证)通过。例如,如果输入为120932,则将显示错误消息,但也会通过。任何帮助将不胜感激,谢谢! :)

<?php
  $fname="";
  $fnameERR="";
  if ($_SERVER["REQUEST_METHOD"] == "POST"){
    if (empty($_POST['fname'])){
      $fnameERR =  "Your name is required";
    } else {
      $fname = test_input($_POST['fname']);
    }
    if (ctype_alpha(str_replace("", "", $fname)) == false){
      $fnameERR = "Only letters are allowed";
    } else {
      $fname = test_input($_POST['fname']);
    }
  }

  function test_input($data){
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    return $data;
  }
  if ($_POST['fname']){
    echo "Your name is ".$fname;
  }
?>
php variables
1个回答
0
投票

正在进行中,是因为您在回显之前没有检查是否有任何错误。更改最后一个if语句,如下所示:

if ($_POST['fname'] && !$fnameERR){
    echo "Your name is ".$fname;
  }
© www.soinside.com 2019 - 2024. All rights reserved.