php完整表单验证实例

<!DOCTYPE HTML>
<html>
<head>
<style>
.error{color:#FF0000}
</style>
</head>
<body>
<?php
	$nameErr=$emailErr=$genderErr=$websiteErr="";
	$name=$email=$comment=$gender=$website="";
	if($_SERVER["REQUEST_METHOD"]=="POST")
	{
		if(empty($_POST["name"]))
		{
			$nameErr="用户名是必须的";
		}
		else
		{
			$name=test_input($_POST["name"]);
			if(!preg_match("/^[a-zA-Z ]*$/",$name))
			{
				$nameErr="用户名只能为数字和空格";
			}
		}
		if (empty($_POST["email"]))
		{
			$emailErr = "邮箱是必填的";
		}
		else
		{
			$email = test_input($_POST["email"]);
			// check if e-mail address syntax is valid
			if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$email))
			{
				$emailErr = "不是邮箱格式"; 
			}
		}
     
		if (empty($_POST["website"]))
		{
			$website = "";
		}
		else
		{
			$website = test_input($_POST["website"]);
			// check if URL address syntax is valid (this regular expression also allows dashes in the URL)
			if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i",$website))
			{
				$websiteErr = "不是url"; 
			}
		}

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

		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;
	}
?>
<h2>php表单验证实例</h2>
<p><span class="error"> * 必须输入的</span></p>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>"> 
   Name: <input type="text" name="name" value="<?php echo $name;?>">
   <span class="error">* <?php echo $nameErr;?></span>
   <br><br>
   E-mail: <input type="text" name="email" value="<?php echo $email;?>">
   <span class="error">* <?php echo $emailErr;?></span>
   <br><br>
   Website: <input type="text" name="website" value="<?php echo $website;?>">
   <span class="error"><?php echo $websiteErr;?></span>
   <br><br>
   Comment: <textarea name="comment" rows="5" cols="40"><?php echo $comment;?></textarea>
   <br><br>
   Gender:
   <input type="radio" name="gender" <?php if (isset($gender) && $gender=="female") echo "checked";?>  value="female">Female
   <input type="radio" name="gender" <?php if (isset($gender) && $gender=="male") echo "checked";?>  value="male">Male
   <span class="error">* <?php echo $genderErr;?></span>
   <br><br>
   <input type="submit" name="submit" value="Submit"> 
</form>

<?php
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>

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的 HTML 和 PHP 表单实例: **HTML 表单** ```html <!DOCTYPE html> <html> <head> <title>PHP 表单实例</title> </head> <body> <h2>请填写以下信息:</h2> <form action="process_form.php" method="post"> <label for="name">姓名:</label> <input type="text" name="name" id="name"><br><br> <label for="email">邮箱:</label> <input type="email" name="email" id="email"><br><br> <label for="gender">性别:</label> <input type="radio" name="gender" value="male" id="male">男 <input type="radio" name="gender" value="female" id="female">女<br><br> <label for="interests">兴趣爱好:</label> <input type="checkbox" name="interests[]" value="reading" id="reading">阅读 <input type="checkbox" name="interests[]" value="sports" id="sports">运动 <input type="checkbox" name="interests[]" value="music" id="music">音乐<br><br> <label for="feedback">反馈意见:</label> <textarea name="feedback" id="feedback"></textarea><br><br> <input type="submit" value="提交"> </form> </body> </html> ``` 在上面的 HTML 表单中,我们使用了文本框、电子邮件、单选按钮、复选框和文本区域等不同类型的输入元素。 **PHP 处理表单** 当用户提交表单时,我们需要处理表单数据。我们可以使用 PHP 来处理表单数据并执行必要的操作。以下是一个简单的 PHP 表单处理代码: ```php <!DOCTYPE html> <html> <head> <title>PHP 表单处理实例</title> </head> <body> <h2>您提交的表单数据如下:</h2> <?php $name = $_POST['name']; $email = $_POST['email']; $gender = $_POST['gender']; $interests = $_POST['interests']; $feedback = $_POST['feedback']; echo "<p>姓名:$name</p>"; echo "<p>邮箱:$email</p>"; echo "<p>性别:$gender</p>"; echo "<p>兴趣爱好:</p>"; echo "<ul>"; foreach ($interests as $value) { echo "<li>$value</li>"; } echo "</ul>"; echo "<p>反馈意见:$feedback</p>"; ?> </body> </html> ``` 在上面的 PHP 代码中,我们使用了 `$_POST` 超级全局变量来获取提交的表单数据。然后,我们使用 `echo` 语句将数据输出到网页中。我们还使用 `foreach` 循环遍历 `$interests` 数组,并将其作为无序列表输出。 请注意,在实际应用中,您可能需要对表单数据进行验证和过滤,以确保输入的数据是有效和安全的。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值