在使用单独的html和php文件提交之前进行PHP表单验证 [英] PHP form validation before submit using separate html and php files

查看:76
本文介绍了在使用单独的html和php文件提交之前进行PHP表单验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做一项任务,我需要使用单独的html和php文件,但无法理解如何验证我的表单输入,因为我发现所有示例都在一个文件中同时包含html和php。



我一直在关注w3schools上的示例: http://www.w3schools.com/php/php_form_validation.asp



我的w3_example.html:

 <!DOCTYPE HTML> 
< html>
< head>
< style>
.error {color:#FF0000;}
< / style>
< / head>
< body>

<?php
//定义变量并设置为空值
函数test_input($ data){
$ data = trim($ data);
$ data = stripslashes($ data);
$ data = htmlspecialchars($ data);
返回$ data;
}
?>

< h2> PHP表单验证示例< / h2>
< p>< span class =error> *必填字段。< / span>< / p>
< form method =postaction =w3_example.php>
名称:< input type =textname =name>
< span class =error> *<?php echo $ nameErr;?>< / span>
< br>< br>
电子邮件:< input type =textname =email>
< span class =error> *<?php echo $ emailErr;?>< / span>
< br>< br>
网站:< input type =textname =website>
< span class =error><?php echo $ websiteErr;?>< / span>
< br>< br>
评论:< textarea name =commentrows =5cols =40>< / textarea>
< br>< br>
性别:
< input type =radioname =gendervalue =female> Female
< input type =radioname =gendervalue = male>男性
< span class =error> *<?php echo $ genderErr;?>< / span>
< br>< br>
< input type =submitname =submitvalue =Submit>
< / form>

< / body>
< / html>

我的w3_example.php:

 <!DOCTYPE HTML> 
< html>
< head>
< / head>
< body>

<?php
//定义变量并设置为空值
$ nameErr = $ emailErr = $ genderErr = $ websiteErr =;
$ name = $ email = $ gender = $ comment = $ website =;

if($ _SERVER [REQUEST_METHOD] ==POST){
if(empty($ _ POST [name])){
$ nameErr =名称是必需的;
} else {
$ name = test_input($ _ POST [name]);


if(empty($ _ POST [email])){
$ emailErr =需要电子邮件;
} else {
$ email = test_input($ _ POST [email]);
}

if(empty($ _ POST [website])){
$ website =;
} else {
$ website = test_input($ _ POST [website]);
}

if(empty($ _ POST [comment])){
$ comment =;
} else {
$ comment = test_input($ _ POST [comment]);


if(空($ _ POST [gender])){
$ genderErr =需要性别;
} else {
$ gender = test_input($ _ POST [gender]);
}
}




echo< h2>您的输入:< / h2>;
echo $ name;
回显< br>;
echo $ email;
回显< br>;
echo $ website;
回显< br>;
echo $ comment;
回显< br>;
echo $ gender;

?>
< / body>
< / html>

$ name = test_input ($ _POST [ 名称]);在w3_example.php中

解决方案


只是一个提示!正如我所能想象的那样,始终关心你学习的地方。 w3schools没有高质量的
代码。使用 http://php.net/manual/en/tutorial.forms.php来自示例
,这是官方的PHP文档。他们的示例代码,评论
等远远好于w3schools。


我再次检查了您的示例代码:



创建2个文件:


  1. index.html / index.php(在本例中它不关心它是.html还是.php格式。

  2. handler.php



<第一个文件: index.html / index.php
$ b

 <!DOCTYPE HTML> 
< html>
< head>
< / head>
< body>

< h2> PHP Form Validation Example< / h2>
< form method =postaction =handler.php>
名称:< input type =textname =name>
< ;< br>< br>
电子邮件:< input type =textname =email>
< br>< br>
网站:< input type =textname =website>
< br>< br>
评论:< textarea name =commentrows =5cols =40> ;< / textarea>
< br> <峰; br>
性别:
< input type =radioname =gendervalue =female> Female
< input type =radioname =gendervalue = 男性>男性
< br>< br>
< input type =submitname =submitvalue =Submit>
< / form>

< / body>
< / html>

您的第二个文件: handler.php

 <?php 
//定义变量并设置为空值
$ name = $ email = $ gender = $ comment = $ website =;

if($ _SERVER [REQUEST_METHOD] ==POST){
$ name = test_input($ _ POST [name]);
$ email = test_input($ _ POST [email]);
$ website = test_input($ _ POST [website]);
$ comment = test_input($ _ POST [comment]);
$ gender = test_input($ _ POST [gender]);
}

函数test_input($ data){
$ data = trim($ data);
$ data = stripslashes($ data);
$ data = htmlspecialchars($ data);
返回$ data;
}

echo< h2>您的输入:< / h2>;
echo $ name;
回显< br>;
echo $ email;
回显< br>;
echo $ website;
回显< br>;
echo $ comment;
回显< br>;
echo $ gender;
?>

此选项将以不同的页面URL形式显示您的输入。



这会起作用!

顺便说一句,我真的不喜欢那个糟糕的示例代码。
请使用:

 <?php 
//定义变量并设置为空值
$ name = $ email = $ gender = $ comment = $ website =;

if(isset($ _ POST [submit])){
$ name = test_input($ _ POST [name]);
$ email = test_input($ _ POST [email]);
$ website = test_input($ _ POST [website]);
$ comment = test_input($ _ POST [comment]);
$ gender = test_input($ _ POST [gender]);

echo< h2>您的输入:< / h2>;
echo $ name;
回显< br>;
echo $ email;
回显< br>;
echo $ website;
回显< br>;
echo $ comment;
回显< br>;
echo $ gender;



函数test_input($ data){
$ data = trim($ data);
$ data = stripslashes($ data);
$ data = htmlspecialchars($ data);
返回$ data;
}

而不是
<$ ($ _SERVER [REQUEST_METHOD] ==POST){
$ name = test_input($ _ POST [name]); pre>
$ email = test_input($ _ POST [email]);
$ website = test_input($ _ POST [website]);
$ comment = test_input($ _ POST [comment]);
$ gender = test_input($ _ POST [gender]);
}


I'm doing an assignment in which I am required to use separate html and php files but am having trouble understanding how to validate my forms input as all examples I've found have both html and php in one file.

I've been following the example on w3schools:http://www.w3schools.com/php/php_form_validation.asp

My w3_example.html:

    <!DOCTYPE HTML> 
<html>
<head>
<style>
.error {color: #FF0000;}
</style>
</head>
<body> 

<?php
// define variables and set to empty values
function test_input($data) {
   $data = trim($data);
   $data = stripslashes($data);
   $data = htmlspecialchars($data);
   return $data;
}
?>

<h2>PHP Form Validation Example</h2>
<p><span class="error">* required field.</span></p>
<form method="post" action="w3_example.php"> 
   Name: <input type="text" name="name">
   <span class="error">* <?php echo $nameErr;?></span>
   <br><br>
   E-mail: <input type="text" name="email">
   <span class="error">* <?php echo $emailErr;?></span>
   <br><br>
   Website: <input type="text" name="website">
   <span class="error"><?php echo $websiteErr;?></span>
   <br><br>
   Comment: <textarea name="comment" rows="5" cols="40"></textarea>
   <br><br>
   Gender:
   <input type="radio" name="gender" value="female">Female
   <input type="radio" name="gender" value="male">Male
   <span class="error">* <?php echo $genderErr;?></span>
   <br><br>
   <input type="submit" name="submit" value="Submit"> 
</form>

</body>
</html>

My w3_example.php:

<!DOCTYPE HTML> 
<html>
<head>
</head>
<body> 

<?php
// define variables and set to empty values
$nameErr = $emailErr = $genderErr = $websiteErr = "";
$name = $email = $gender = $comment = $website = "";

if ($_SERVER["REQUEST_METHOD"] == "POST") {
   if (empty($_POST["name"])) {
     $nameErr = "Name is required";
   } else {
     $name = test_input($_POST["name"]);
   }

   if (empty($_POST["email"])) {
     $emailErr = "Email is required";
   } else {
     $email = test_input($_POST["email"]);
   }

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

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

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




echo "<h2>Your Input:</h2>";
echo $name;
echo "<br>";
echo $email;
echo "<br>";
echo $website;
echo "<br>";
echo $comment;
echo "<br>";
echo $gender;

?>
</body>
</html>

I now get an "Call to undefined function test_input()" error on this line $name = test_input($_POST["name"]); in w3_example.php

解决方案

Just a tip! Because you studying as I can imagine, always care about from where you study. w3schools doesn't have a good quality code. Use http://php.net/manual/en/tutorial.forms.php from example which is the official PHP documentation. Their sample codes, comments etc. are by far better than w3schools.

I checked again your sample code:

Create 2 files:

  1. index.html / index.php (in this example it doesn't care if it is .html or .php format.
  2. handler.php

You 1st file: index.html / index.php

<!DOCTYPE HTML>
<html>
<head>
</head>
<body>

<h2>PHP Form Validation Example</h2>
<form method="post" action="handler.php">
   Name: <input type="text" name="name">
   <br><br>
   E-mail: <input type="text" name="email">
   <br><br>
   Website: <input type="text" name="website">
   <br><br>
   Comment: <textarea name="comment" rows="5" cols="40"></textarea>
   <br><br>
   Gender:
   <input type="radio" name="gender" value="female">Female
   <input type="radio" name="gender" value="male">Male
   <br><br>
   <input type="submit" name="submit" value="Submit">
</form>

</body>
</html>

Your 2nd file: handler.php

<?php
// define variables and set to empty values
$name = $email = $gender = $comment = $website = "";

if ($_SERVER["REQUEST_METHOD"] == "POST") {
   $name = test_input($_POST["name"]);
   $email = test_input($_POST["email"]);
   $website = test_input($_POST["website"]);
   $comment = test_input($_POST["comment"]);
   $gender = test_input($_POST["gender"]);
}

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

echo "<h2>Your Input:</h2>";
echo $name;
echo "<br>";
echo $email;
echo "<br>";
echo $website;
echo "<br>";
echo $comment;
echo "<br>";
echo $gender;
?>

This option will show your inputs in the form in a different page-URL.

That will work!

By the way, I really don't like that poor sample code. Please, use:

<?php
// define variables and set to empty values
$name = $email = $gender = $comment = $website = "";

if (isset($_POST["submit"])){
       $name = test_input($_POST["name"]);
       $email = test_input($_POST["email"]);
       $website = test_input($_POST["website"]);
       $comment = test_input($_POST["comment"]);
       $gender = test_input($_POST["gender"]);

        echo "<h2>Your Input:</h2>";
        echo $name;
        echo "<br>";
        echo $email;
        echo "<br>";
        echo $website;
        echo "<br>";
        echo $comment;
        echo "<br>";
        echo $gender;

    }

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

instead of

if ($_SERVER["REQUEST_METHOD"] == "POST") {
   $name = test_input($_POST["name"]);
   $email = test_input($_POST["email"]);
   $website = test_input($_POST["website"]);
   $comment = test_input($_POST["comment"]);
   $gender = test_input($_POST["gender"]);
}

这篇关于在使用单独的html和php文件提交之前进行PHP表单验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆