gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 00:46:11 25 4
gpt4 key购买 nike

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

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

friend 们帮帮我

<?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;
}

?>

最佳答案

当名字包含除字母和空格之外的额外字符时,您需要将 $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
}

关于php 第二个如果验证不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21347931/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com