【问题标题】:Pass variable from one script to another on same page?在同一页面上将变量从一个脚本传递到另一个脚本?
【发布时间】:2013-08-17 17:59:16
【问题描述】:

全部,

我一直在为此苦苦挣扎,但我不知道自己做错了什么。我有一个 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>

【问题讨论】:

  • 那么,您是否尝试在底部使用$reset_question?你有错误还是什么?
  • 是的,我已经尝试了所有我能想到的从一个传递到另一个的组合。当我使用 $reset_question 时,它不会返回任何结果。 $_SESSION['$reset_question'] 给了我一个尖叫错误,并说“$reset_question”是一个未定义的索引。

标签: php jquery html sql


【解决方案1】:

看起来你的变量 $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'];
    //....
}

【讨论】:

  • 我只是在 while 循环之外使用 $reset_question 进行了尝试,但仍然没有运气。我确实将其更改为 $reset_question 而不是底部的 $_SESSION['$reset_question'] ,但它仍然不起作用。
  • 我都试过了 $reset_question;和 $reset_question = '': 都没有解决问题。
  • 要尝试弄清楚发生了什么,您可以在代码顶部硬编码 reset_question 变量,然后尝试在代码中进一步使用它。您需要弄清楚哪个部分不起作用。 while 循环没有执行任何代码吗?还是什么?
  • 我已经尝试了很多 $reset_question; 的组合。和 $reset_question = '';。我要么得到一个完全空白的屏幕,要么出现以下“请回答以下问题!什么是:?”页面永远不会从本地主机完全加载。我得到了死亡之轮。
【解决方案2】:

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

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

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

如果您的条件中 $reset_question 中没有任何内容,那么您将收到“默认问题”

更新:再试一次,我相信你至少会得到“什么是默认问题?” 在$error_msg = ""; 之后写$reset_question = 'Defaut Question';

$error_msg = "";
$reset_question = 'Defaut Question';

【讨论】:

  • 我在脚本中能想到的所有地方都试过了,但它不起作用。我要么得到一个完全空白的屏幕,要么出现以下“请回答以下问题!什么是:?”页面永远不会从本地主机完全加载。
  • 现在试试你不会得到一个空白屏幕。现在你至少会得到“什么是:默认问题?”
  • 我不知道我做错了什么,但是当我尝试以上所有内容时,我得到的是“什么是:?”
  • 对不起萨米。它仍然没有工作。我不认为我的 WAMP 安装有问题,但现在我得到的只是页面选项卡上的加载轮。它仍然显示“什么是:?”
最近更新 更多