我无法在不同的页面上发布回发数据

时间:2014-05-24 09:18:08

标签: php html mysql

我已经创建了一个register.php页面,它有用于用户输入的html表单,然后发布到自己,php验证输入,这一切都按我的意愿工作。我可以在验证后回复数据而没有问题,但是当我使用头重定向添加重定向到register.inc.php然后尝试从register.inc.php页面回显帖子数据时它不会这样做,几乎就像数据没有发布到register.inc.php文件,但是,如果我在register.php页面中包含register.inc.php,它将回显register.php页面上的数据,这肯定表明正在发送帖子数据,最终我希望将这些数据输入数据库,但很明显,如果后期数据没有被看到我无法做到,我已经尝试了一个会话开始没有工作,我似乎无法通过任何研究解决这个问题我已经完成了,我认为这是一件非常简单的事情。下面是registration.php页面和register.inc.php页面,我无法添加图像,因为我没有足够的分数。

register.php

    <?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>&nbsp;</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">&nbsp;</td>
              </tr>
            </table>
          </fieldset>
          </form>
    </div>



</body>
</html>

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/>';

?>

3 个答案:

答案 0 :(得分:0)

最好的办法是在register.inc.php中编写整个代码块,然后在其中包含它并在其后面加上一个die()语句。

答案 1 :(得分:0)

一种方法是通过url传递数据,就像

一样
    register.inc.php?name=abcd&email=abcd@gmail.com

并使用$_GET['username'];中的$_GET['email'];register.inc.php方法获取数据。

希望这有帮助。

答案 2 :(得分:0)

如果要在页面之间传输数据,请使用register.inc.php和register.php页面开头的session_start();。 然后你可以在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

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/>';