Php 我无法将post数据回送到不同的页面
我已经创建了一个register.php页面,它有一个用于用户输入的html表单,然后将其发布到php验证输入的地方,所有这些都可以按照我的要求工作。我可以在验证后无问题地回显数据,但是,当我使用头重定向添加重定向到register.inc.php,然后尝试回显register.inc.php页面中的post数据时,它不会这样做,就像数据没有被发布到register.inc.php文件一样,但是,如果我将register.inc.php包含在register.php页面中,它将响应register.php页面上的数据,这肯定表明正在发送post数据,最终我希望将此数据输入数据库,但显然,如果没有看到post数据,我将无法,我尝试了一个不起作用的会议开始,我似乎无法通过我所做的任何研究来解决这个问题,我打赌这是我错过的非常简单的事情。下面是registration.php页面和register.inc.php页面,我无法添加图像,因为我没有足够的分数 register.phpPhp 我无法将post数据回送到不同的页面,php,html,mysql,Php,Html,Mysql,我已经创建了一个register.php页面,它有一个用于用户输入的html表单,然后将其发布到php验证输入的地方,所有这些都可以按照我的要求工作。我可以在验证后无问题地回显数据,但是,当我使用头重定向添加重定向到register.inc.php,然后尝试回显register.inc.php页面中的post数据时,它不会这样做,就像数据没有被发布到register.inc.php文件一样,但是,如果我将register.inc.php包含在register.php页面中,它将响应registe
<?php
//include_once 'includes/register.inc.php';
// define variables and set to empty values
$usernameErr = $emailErr = $passwordErr = $clubnameErr = "";
$username = $email = $password = $clubname = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$username = test_input($_POST["name"]);
$email = test_input($_POST["email"]);
$password = test_input($_POST["password"]);
$clubname = test_input($_POST["clubname"]);
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["username"])) {
$usernameErr = "**Name is required**";
} else {
$username = test_input($_POST["username"]);
if (!preg_match("/^[a-zA-Z ]*$/",$username)) {
$usernameErr = "**Only letters and white space allowed**";
exit();
}
}
if (empty($_POST["email"])) {
$emailErr = "**Email is required**";
} else {
$email = test_input($_POST["email"]);
if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$email)) {
$emailErr = "**Invalid email format**";
}
}
if (empty($_POST["password"])) {
$passwordErr = "**Password is required**";
} else {
$password = test_input($_POST["password"]);
}
if (empty($_POST["confirmpassword"])) {
$confirmpasswordErr = "**Please confirm password**";
} else {
$confirmpassword = test_input($_POST["confirmpassword"]);
}
if($_POST['password'] != $_POST['confirmpassword']){
$confirmpasswordErr = "**Passwords did not match**";
}
if($_POST["password"] && strlen(trim($_POST["password"])) < 6)
{
$passwordErr = "**Password needs to be at least 6 characters**";
}
if (empty($_POST["clubname"])) {
$clubnameErr = "**Club Name is required**";
} else {
$clubname = test_input($_POST["clubname"]);
if (!preg_match("/^[a-zA-Z ]*$/",$clubname)) {
$clubnameErr = "**Only letters and white space allowed**";
}
if ($usernameErr == '' && $emailErr == '' && $passwordErr == '' && $confirmpasswordErr == '' && $clubnameErr == '') {
$clubname;
header("Location: includes/register.inc.php");
}
}
}
//echo $_POST['username'];
//echo $_POST['email'];
//echo $_POST['password'];
//echo $_POST['confirmpassword'];
//echo $_POST['clubname'];
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
<style type="text/css">
#form {
width: 80%;
margin-left: auto;
margin-right: auto;
}
</style>
</head>
<body>
<div id="form">
<form id="registermember" name="registermember" method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<div align="center">
<fieldset>
<legend style="font-weight: bolder; font-size: large;">Register Members</legend>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="33%"><div align="right"><span style="color: #000000; font-weight: bold;">Members Name:</span></div></td>
<td width="33%"><div align="center"><span style="color: #000000; font-weight: bold;">
<input name="username" type="text" id="username" value="<?php echo $username;?>" size="40%">
</span></div>
<td width="33%"><div align="left"><span class="error" style="color: #FF0004; font-weight: bold;"><?php echo $usernameErr;?></span></div></td>
</tr>
<tr>
<td><div align="center"><span style="color: #000000; font-weight: bold;">
</span></div> <span style="color: #000000; font-weight: bold;">
<label for="email">
<div align="center">
<div align="right"><span style="color: #000000; font-weight: bold;">Members Email:</span></div>
</div>
<span style="color: #000000; font-weight: bold;"></span><span style="color: #000000; font-weight: bold;">
</label>
</span></td>
<td><div align="center"><span style="color: #000000; font-weight: bold;">
<input name="email" type="text" id="email" value="<?php echo $email;?>" size="40%">
</span></div>
<td style="color: #FF0004"><div align="left"><span style="color: #FF0004; font-weight: bold;"><span class="error"><?php echo $emailErr;?></span></span></div></td>
</tr>
<tr>
<td><div align="center"><span style="color: #000000; font-weight: bold;">
</span></div> <span style="color: #000000; font-weight: bold;">
<label for="password2">
<div align="center">
<div align="right"><span style="color: #000000; font-weight: bold;">Members Password:</span></div>
</div>
<span style="color: #000000; font-weight: bold;"></span><span style="color: #000000; font-weight: bold;">
</label>
</span></td>
<td><div align="center"><span style="color: #000000; font-weight: bold;">
<input name="password" type="password" id="password" size="40%">
</span></div>
<td style="color: #FF0004"><div align="left"><span style="color: #FF0004; font-weight: bold;"><span class="error"><?php echo $passwordErr;?></span></span></div></td>
</tr>
<tr>
<td><div align="center"><span style="color: #000000; font-weight: bold;">
</span></div> <span style="color: #000000; font-weight: bold;">
<label for="confirmpassword">
<div align="center">
<div align="right"><span style="color: #000000; font-weight: bold;">Confirm Password:</span></div>
</div>
<span style="color: #000000; font-weight: bold;"></span><span style="color: #000000; font-weight: bold;">
</label>
</span></td>
<td><div align="center"><span style="color: #000000; font-weight: bold;">
<input name="confirmpassword" type="password" id="confirmpassword" size="40%">
</span></div>
<td style="color: #FF0004"><div align="left"><span style="color: #FF0004; font-weight: bold;"><span class="error"><?php echo $confirmpasswordErr;?></span></span></div></td>
</tr>
<tr>
<td><div align="center"><span style="color: #000000; font-weight: bold;">
</span></div> <span style="color: #000000; font-weight: bold;">
<label for="clubname2">
<div align="center">
<div align="right"><span style="color: #000000; font-weight: bold;">Members Club:</span></div>
</div>
<span style="color: #000000; font-weight: bold;"></span><span style="color: #000000; font-weight: bold;">
</label>
</span></td>
<td><div align="center"><span style="color: #000000; font-weight: bold;">
<input name="clubname" type="text" id="clubname" value="<?php echo $clubname;?>" size="40%">
</span></div>
<td style="color: #FF0004"><div align="left"><span style="color: #FF0004; font-weight: bold;"><span class="error"><?php echo $clubnameErr;?></span></span></div></td>
</tr>
<tr>
<td> </td>
<td><div align="center">
<p>
<input name="submitmember" type="submit" id="submitmember" formaction="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" formmethod="POST" value="Add Member">
</p>
</div>
<td style="color: #FF0004"> </td>
</tr>
</table>
</fieldset>
</form>
</div>
</body>
</html>
无标题文件
#形式{
宽度:80%;
左边距:自动;
右边距:自动;
}
最好的方法是在register.inc.php中编写整个代码块,在其中包含它,并在后面有一个die()语句。一种方法是通过url传递数据,就像
register.inc.php?name=abcd&email=abcd@gmail.com
并使用$\u GET['username']代码>和$\u获取['email']使用register.inc.php
中的code>方法获取数据
希望这有帮助。如果您想在页面之间传输数据,请使用session_start()代码>位于register.inc.php和register.php页面的开头。
然后可以在register.inc.php上调用它
此时,您需要这样做:
if ($usernameErr == '' && $emailErr == '' && $passwordErr == '' && $confirmpasswordErr == '' && $clubnameErr == '') {
$_SESSION['data'] = array($_POST['username'],$_POST['email'],$_POST['password'],$_POST['clubname']);
header("Location: includes/register.inc.php");
//insert post data to session var.
}
然后在register.inc.php
<?php
$username = $_POST['username'];
$email = $_POST['email'];
$password = $_POST['password'];
$clubname = $_POST['clubname'];
echo 'success'.'<br/><br/>';
echo $clubname.'<br/><br/>';
echo $username.'<br/><br/>';
echo $email.'<br/><br/>';
echo $password.'<br/><br/>';
?>
list($username,$email,$pass,$clubname) = $_SESSION['data'];
unset($_SESSION['data']);
//expand it on list then echo.
echo 'success'.'<br/><br/>';
echo $clubname.'<br/><br/>';
echo $username.'<br/><br/>';
echo $email.'<br/><br/>';
echo $password.'<br/><br/>';
list($username、$email、$pass、$clubname)=$\u会话['data'];
取消设置($_会话['data']);
//在列表中展开它,然后回显。
回应“成功”。
;
echo$clubname.“
”;
回显$username。“
”;
回显$email.“
”;
回显$password。“
”;
Post数据在重定向时不会保留。。。。不是那样的。您可能应该包括register.inc.php文件,而不是重定向到它。我已经这样做了,但它会直接回显到register页面,是否有任何方法可以阻止它这样做?我试过了,register.inc.php仍然不会回显数据,除非返回到register.php页面我这样做的原因是为了避免未经修改的密码,一旦我知道数据被发布到register.inc.php页面,我会在register.php页面上散列密码,除非我的想法是错误的