0

全部,

我一直在为此苦苦挣扎,我不知道自己做错了什么。我有一个 PHP 文件,其中包含多个脚本,包括 PHP 和 jquery 部分。我正在尝试将一个 PHP 变量从 html Head 部分传递给 Body。每个都在自己的 php 脚本部分,因为我在两者之间有一个 jquery 脚本,也在 Head 中。下面是相关代码。如何将 $reset_question php 变量从顶部传递到底部?

我刚刚添加了“提交 3”按钮来显示我遇到问题的表单。也许在我的语法中?

<head>
<?php
  require_once('../connectvars.php');
  session_start();
  $dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
  // Clear the error message
  $error_msg = "";

  // other code that I'm not having a problem with

if (!isset($_SESSION['email'])) {
if (isset($_POST['submit3'])) {

  // Grab the user-entered log-in data
  $email = mysqli_real_escape_string($dbc, trim($_POST['email']));
  $first_name = mysqli_real_escape_string($dbc, trim($_POST['first_name']));
  $last_name = mysqli_real_escape_string($dbc, trim($_POST['last_name']));

  if (!empty($first_name) && !empty($last_name) && !empty($email) ) {
    // Make sure someone isn't already registered using this username
    $query = "SELECT * FROM user_database WHERE email = '$email' AND first_name = '$first_name' AND last_name = '$last_name'";
    $data = mysqli_query($dbc, $query);
    if (mysqli_num_rows($data) == 1) {
      // The username exists
      $query = "SELECT reset_question FROM user_database where email='$email'";
      mysqli_query($dbc, $query);

      // Confirm success with the user
      while($row = mysqli_fetch_array($data)) {
        $reset_question = $row['reset_question'];
      }
        exit();
      }
      else {
      // An account doesn't exist for this e-mail
        echo '<p class="error">All of your information was not recognized. Please complete the information correctly or sign-up to register.</p>';
        $email = "";
      }
    }
    else {  
      echo '<p class="error">You must enter all of the required data.</p>';
    }
$_SESSION['reset_question'] = $reset_question;
  }
}


// Insert the page header
require_once('../reference/header_sub.php');

// If the session var is empty, show any error message and the log-in form; otherwise confirm the log-in
if (empty($_SESSION['email'])) {
echo '<p class="error">' . $error_msg . '</p>';
// closing bracket is down below 
?>

// other code that I'm not having a problem with

//jquery script
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"> </script>
<script>

// jquery isn't having any issues that I can see

</script>

</head>

<body>

<div id="allcontent" style="position:relative;top:-20px;">

<?php
// Insert the tabbed navigation
  require_once('../reference/tabs_sub.php');
?>

<br />

<fieldset> 

<!-- html forms that I've not having problems with -->

<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
  <table class="reset">
  <tr><td colspan="2" ><legend style="font-weight:bold;font-size:15px;height:25px;">Reset your password</legend></td></tr>
  <tr><td class="register" ><label for="first_name">First Name:</label></td>
  <td><input style="width:200px;" type="text" name="first_name" value="<?php if (!empty($first_name)) echo $first_name; ?>" /><br /></td></tr>
  <tr><td class="register" ><label for="last_name">Last Name:</label></td>
  <td><input style="width:200px;" type="text" name="last_name" value="<?php if (!empty($last_name)) echo $last_name; ?>" /><br /></td></tr>
  <tr><td class="register" ><label for="email">E-mail:</label></td>
  <td><input style="width:200px;" type="text" name="email" value="<?php if (!empty($email)) echo $email; ?>" /><br /></td><td><input type="submit" value="Submit" name="submit3" class="submit3"/></td></tr>
  </table>
</form>

<form method="post" action="<?php echo $_SERVER["PHP_SELF"]; ?>">  
  <table class="answer">
    <tr><td colspan="2" class="remember" >Please answer the following question!</td></tr>   
    <tr><td class="remember" >What is: <?php $_SESSION['reset_question']; ?>?</td></tr>
    <tr><td ><input style="width:200px;" type="text" name="reset_answer" value="<?php if (!empty($reset_answer)) echo $reset_answer; ?>"/></td></tr>
  </table>
</form>

</fieldset>

<?php
} // closing bracket from above opening bracket
else {
// Confirm the successful log-in
  echo('<p class="login">You are logged in as ' . $_SESSION['email'] . '.</p>');
  require_once('/download.php');
}
?>

<?php
// Insert the page footer
require_once('../reference/footer.php');
mysqli_close($dbc);
?>

</div>

</body>
4

2 回答 2

0

从任何括号中声明您的变量。只需在 php 脚本范围内,您就可以在文件中的任何位置获取它,在较低的脚本中传递(访问)它不需要其他任何东西。

声明/初始化它的最佳位置是之前

$reset_question = 'Defaut Question';
if (!empty($first_name) && !empty($last_name) && !empty($email) )
{

如果您在 $reset_question 中没有得到任何条件,那么您将获得“默认问题”

Upadte:再试一次,我相信你至少会得到“什么是默认问题?” 紧随$reset_question = 'Defaut Question';其后$error_msg = "";_

$error_msg = "";
$reset_question = 'Defaut Question';
于 2013-08-17T18:14:17.190 回答
0

看起来您的变量 $reset_question 仅存在于 while 循环的范围内

while($row = mysqli_fetch_array($data)) {
    $reset_question = $row['reset_question'];
    //....
}

而是在 while 循环之外初始化变量。

$reset_question = '';
while($row = mysqli_fetch_array($data)) {
    $reset_question = $row['reset_question'];
    //....
}
于 2013-08-17T18:05:53.887 回答