doulangbizhan5160 2014-01-04 18:19 采纳率: 100%
浏览 53
已采纳

表单验证不停止无效的输入。将用户发送到标题位置。(PHP)

I found this, Form submission will not stop for validation, but it is based of of JQUERY and not PHP. Should I not use PHP and switch?

So I have what I think to be a validated form. Problem is that if the form information is not right, my site will go ahead and forward the registrar to the next page. I will leave my form code below, and maybe you guys who are better at this than me, will have the kindness and compassion to help me as usual. After that I will show you how I am processing the information. Thanks for all you guys do. I am just very confused. Thanks again and happy 2014.

if ($_SERVER["REQUEST_METHOD"] == "POST")
{
if (empty($_POST["first_name"]))
{$first_nameErr = "Required";}
else
{
$first_name = test_input($_POST["first_name"]);
// check if name only contains letters and whitespace
if (!preg_match("/^[a-zA-Z ]*$/",$first_name))
{
$first_nameErr = "Only letters allowed";
}
}

if (empty($_POST["last_name"]))
{$last_nameErr = "Required";}
else
{
$last_name = test_input($_POST["last_name"]);
// check if name only contains letters and whitespace
if (!preg_match("/^[a-zA-Z ]*$/",$last_name))
{
$last_nameErr = "Only letters allowed";
}
}


if (empty($_POST["email"]))
{$emailErr = "Required";}
else
{
$email = test_input($_POST["email"]);
// check if e-mail address syntax is valid
if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]...
{
$emailErr = "Invalid email format";
}
}


if (empty($_POST["password"]))
{$passwordErr = "Required";}
else
{$password = test_input($_POST["password"]);}

if (empty($_POST["gender"]))
{$genderErr = "";}
else
{$gender = test_input($_POST["gender"]);}

}

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



<div class="signupitsfree" float="right">
<p class="signup">Sign Up
It's Completely Free!</p>


<form method="POST" name="signup" action="life/chickenwings.php">

<label for="first name"></label><input id="first name" name="first_name"   
placeholder="First Name" type="text" value="<?php echo $first_name;?>" /> <span 
class="error">* <?php echo $first_nameErr;?></span>

<label for="last_name"></label><input id="last name" name="last_name" 
placeholder="Last Name" type="text" value="<?php echo $last_name;?>" />
<span class="error">* <?php echo $last_nameErr;?></span>
<br><br>


<label for="email"></label><input id="email" name="email" placeholder="Email"    
type="text" value="<?php echo $email;?>" />
<span class="error">* <?php echo $emailErr;?></span>





<label for="password"></label><input id="password" name="password" placeholder="Create 
Password" type="password" />
<span class="error">* <?php echo $passwordErr;?></span>



<label for="male"><strong>Male</strong></label> <input id="male" value="male" <?php if 
(isset($gender) && $gender=="male") echo "checked";?> name="gender" type="radio" />
<label for="female"><strong>Female</strong></la... <input id="female" value="female"  
<?php if (isset($gender) && $gender=="female") echo "checked";?> name="gender" 
type="radio" />
<span class="error">* <?php echo $genderErr;?></span>



<label for="submit">"I Agree To <a href="#">Terms And Conditions"</a></label> <input 
id="submit" value="Submit" type="submit" name="submit"/>


<p><span class="error">* required field.</span></p>
<hr>



Now is the way I am processing the information.

<?php

 $hostname="figureitout";
 $username="figureitout";
 $password="figureitout";
 $dbname="figureitout";

  $db_conx = mysqli_connect($hostname, $username, $password) OR DIE ("Unable to
  connect to database! Please try again later.");

  if(mysqli_connect_errno()){
  echo mysqli_connect_error();
  exit();
  }

 $select = mysqli_select_db($db_conx,$dbname);

 $first_name= $_POST["first_name"];
 $last_name= $_POST["last_name"];
 $email= $_POST["email"];
 $password= $_POST["password"];
 $gender= $_POST["gender"];

 mysqli_query($db_conx,"INSERT INTO users (firstname, lastname, email, password, 
 gender)
 VALUES ('$first_name', '$last_name', '$email', '$password', '$gender')");
 mysqli_close($db_conx);

 header("Location: chicken/wing.php")
 ?>
  • 写回答

2条回答 默认 最新

  • doudu7626 2014-01-04 18:29
    关注

    All your validation code needs to be on the SECOND page, before you write the data. PHP is run on the server, so having it above the form is useless (unless the form submits to itself, in which case all this code should be on one page).

    I'm not going to write the whole thing for you, but it should look like this:

    $ errors = 0;
    if ($_SERVER["REQUEST_METHOD"] == "POST")
    {
    if (empty($_POST["first_name"]))
    {$first_nameErr = "Required";
    $errors++;}
    else
    {
    $first_name = test_input($_POST["first_name"]);
    // check if name only contains letters and whitespace
    if (!preg_match("/^[a-zA-Z ]*$/",$first_name))
    {
    $first_nameErr = "Only letters allowed";
    $errors++;
    }
    }
    
    if ($errors) {
     //display form with errors highlighted
    }
    else 
    {
     //do your insert code
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数