PHP表单验证(不知道如何将其拆分为两个不同的页面)
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了PHP表单验证(不知道如何将其拆分为两个不同的页面),小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含9292字,纯文字阅读大概需要14分钟。
内容图文
![PHP表单验证(不知道如何将其拆分为两个不同的页面)](/upload/InfoBanner/zyjiaocheng/663/5a71ee2df60e4a98be075e5b7d904ace.jpg)
我已经尝试了一段时间,以获取HTML表单以提交给PHP表单,以验证其文本字段,如果空白但无法进行任何操作,则必须填写.我也不知道AJAX,否则会尝试这样做.
因此,已在HTML页面内采用PHP自身形式.
这是当前版本:
<?php
// define variables and set to empty values
$firstNameErr = $lastNameErr = $emailErr = $messageErr = "";
$first_name = $last_name = $email = $message = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["first-name"])) {
$firstNameErr = "First name is required";
} else {
$first_name = test_input($_POST["first-name"]);
}
if (empty($_POST["last-name"])) {
$lastNameErr = "Last name is required";
} else {
$last_name = test_input($_POST["last-name"]);
}
if (empty($_POST["email"])) {
$emailErr = "Email is required";
} else {
$email = test_input($_POST["email"]);
}
if (empty($_POST["message"])) {
$messageErr = "Message is required";
} else {
$message = test_input($_POST["message"]);
}
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
这是同一页上的表格:
<form class="ui form" method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<div class="field">
<label>First Name</label>
<input name="first-name" id="first-name" placeholder="First Name" type="text">
<span class="error">* <?php echo $firstNameErr;?></span>
</div>
<div class="field">
<label>Last Name</label>
<input name="last-name" id="last-name" placeholder="Last Name" type="text">
<span class="error">* <?php echo $lastNameErr;?></span>
</div>
<div class="field">
<label>Email</label>
<input name="email" id="email" placeholder="Email" type="email">
<span class="error">* <?php echo $emailErr;?></span>
</div>
<div class="field">
<label>Message</label>
<textarea rows="2" placeholder="Please type in your message" name="message" id="message"></textarea>
<span class="error">* <?php echo $messageErr;?></span>
</div>
<button class="ui button" type="submit">Submit</button>
</form>
有没有办法在两个不同的页面中执行此操作,即在第一页中形成表单,并在会话模式下将其转移到第二页?还是对于商业站点可以接受?请告诉我.谢谢.
附录
if (empty($_POST["email"])) {
$emailErr = "Email is required";
} else {
$email = filter_vars(test_input($_POST["email"], FILTER_VALIDATE_EMAIL));
}
附录2
我已经在php的底部,即在函数test_input.header(‘location:php_mailer_form.php’);之后添加了它.那是正确的位置吗?
但是由于某种原因,当我尝试访问contact.php表单时,它甚至没有显示,只是直接进入了php_mailer_form.php底部的错误表单.
if(!$mail->send()) {
header('location: url/contactError.html');
} else {
header('location: url/contactResult.html');
}
为什么? (请让我知道是否需要添加其他信息).
附录3
<?php
session_start();
$first_name = $_SESSION['first-name'];
$last_name = $_SESSION['last-name'];
$email = $_SESSION['email'];
$message = nl2br($_SESSION['message']);
require 'PHPMailerAutoload.php';
$mail = new PHPMailer;
//$mail->SMTPDebug = 3; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'host_specified'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'email_specified'; // SMTP username
$mail->Password = 'password_specified'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587;
$mail->addReplyTo( $email, $first_name );
$mail->addAddress( $email, $first_name );
$mail->addAddress( 'email_specified', 'Staff' );
$mail->From = 'email_specified';
$mail->FromName = 'Staff';
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Hotel Room Request';
$mail->Body = $message;
$mail->AltBody = 'To view the message, please use an HTML compatible email viewer!';
if(!$mail->send()) {
header('location: url/contactError.html');
} else {
header('location: url/contactResult.html');
}
附录4
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Contact</title>
<!--[if lt IE 9]>
<script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<style type="text/css">
.ui.fixed.sticky + p {
margin-top: 39px;
}
.error {
color: #FF0000;
}
</style>
</head>
<body>
<?php
session_start(); //allows use of session variables
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (!isset($_POST["first-name"])) {
$firstNameErr = "First name is required";
} else {
$first_name = test_input($_POST["first-name"]);
}
if (!isset($_POST["last-name"])) {
$lastNameErr = "Last name is required";
} else {
$last_name = test_input($_POST["last-name"]);
}
if (!isset($_POST["email"])) {
$emailErr = "Email is required";
} else {
$email = test_input($_POST["email"]);
}
if (!isset($_POST["message"])) {
$messageErr = "Message is required";
} else {
$message = test_input($_POST["message"]);
}
if(isset($first_name) && isset($last_name) && isset($email) && isset($message))
{
$_SESSION['first_name'] = $first_name;
$_SESSION['last_name'] = $last_name;
$_SESSION['email'] = $email;
$_SESSION['message'] = $message;
header("Location: contact9Sessions.php");
}
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
<div class="ui container">
<div class="ui segment">
<div>
<div class="ui fluid five item tabular menu">
<a class="item" href="index.html">Home</a>
<a class="item" href="about.html">About</a>
<a class="item" href="rooms.html">Rooms Info & Rates</a>
<a class="item" href="book.html">To Book</a>
<a class="item" href="contact.html">Contact</a>
</div>
</div>
<div class="ui two column stackable grid">
<div class="ten wide column">
<form class="ui form" method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<div class="field">
<label>First Name</label>
<input name="first-name" id="first-name" placeholder="First Name" type="text">
<?php if(isset($firstNameErr)) print ('<span class="error">* ' . $firstNameErr . '</span>'); ?>
</div>
<div class="field">
<label>Last Name</label>
<input name="last-name" id="last-name" placeholder="Last Name" type="text">
<?php if(isset($lastNameErr)) print ('<span class="error">* ' . $lastNameErr . '</span>'); ?>
</div>
<div class="field">
<label>Email</label>
<input name="email" id="email" placeholder="Email" type="email">
<?php if(isset($emailErr)) print ('<span class="error">* ' . $emailErr . '</span>'); ?>
</div>
<div class="field">
<label>Message</label>
<textarea rows="2" placeholder="Please type in your message" name="message" id="message"></textarea>
<?php if(isset($messageErr)) print ('<span class="error">* ' . $messageErr . '</span>'); ?>
</div>
<button class="ui button" type="submit">Submit</button>
</form>
</div>
<div class="six wide column">
<br><br>
<img class="ui centered large bordered rounded image" src="images/tobereplaced.jpg">
</div>
</div>
</div>
<div class="ui two column grid">
<div class="ui left aligned ">
<p>Left Footer Stuff Here</p>
</div>
<div class="ui right aligned">
<p>Right Footer Stuff Here</p>
</div>
</div>
</div>
</body>
</html>
解决方法:
<?php
session_start(); //allows use of session variables
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (!isset($_POST["first-name"])) {
$firstNameErr = "First name is required";
} else {
$first_name = test_input($_POST["first-name"]);
}
if (!isset($_POST["last-name"])) {
$lastNameErr = "Last name is required";
} else {
$last_name = test_input($_POST["last-name"]);
}
if (!isset($_POST["email"])) {
$emailErr = "Email is required";
} else {
$email = test_input($_POST["email"]);
}
if (!isset($_POST["message"])) {
$messageErr = "Message is required";
} else {
$message = test_input($_POST["message"]);
}
if(isset($first_name) && isset($last_name) && isset($email) && isset($message))
{
$_SESSION['first_name'] = $first_name;
$_SESSION['last_name'] = $last_name;
$_SESSION['email'] = $email;
$_SESSION['message'] = $message;
header("Location: php_mailer_form.php");
}
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
<form class="ui form" method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<div class="field">
<label>First Name</label>
<input name="first-name" id="first-name" placeholder="First Name" type="text">
<?php if(isset($firstNameErr)) print ('<span class="error">* ' . $firstNameErr . '</span>'); ?>
</div>
<div class="field">
<label>Last Name</label>
<input name="last-name" id="last-name" placeholder="Last Name" type="text">
<?php if(isset($lastNameErr)) print ('<span class="error">* ' . $lastNameErr . '</span>'); ?>
</div>
<div class="field">
<label>Email</label>
<input name="email" id="email" placeholder="Email" type="email">
<?php if(isset($emailErr)) print ('<span class="error">* ' . $emailErr . '</span>'); ?>
</div>
<div class="field">
<label>Message</label>
<textarea rows="2" placeholder="Please type in your message" name="message" id="message"></textarea>
<?php if(isset($messageErr)) print ('<span class="error">* ' . $messageErr . '</span>'); ?>
</div>
<button class="ui button" type="submit">Submit</button>
</form>
现在,在另一个页面中,要访问变量,只需调用“ session_start();”即可.在页面顶部(如我们在此所做的那样),然后使用调用“ $_SESSION [‘message’]”获得message的值.这回答了你的问题了吗?另请注意,我编辑了html,以便仅在设置了错误变量的情况下才显示错误消息div.
内容总结
以上是互联网集市为您收集整理的PHP表单验证(不知道如何将其拆分为两个不同的页面)全部内容,希望文章能够帮你解决PHP表单验证(不知道如何将其拆分为两个不同的页面)所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。