将变量从一个PHP页面发送到另一个 [英] Sending variables from one PHP page to another

查看:111
本文介绍了将变量从一个PHP页面发送到另一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个从我的服务器上的数据库动态生成的测验Web应用程序。我几乎实现了所有的功能,唯一缺失的部分是使它能够提供多个测验。现在我必须在quiz.php脚本中手动编写代码 $ quiz_id = 1,$ quiz_title =geography,以使其正确运行。如果我可以生成这两个变量的值,我的quiz.php将适用于多个测验题目。

I'm building a quiz web application that is dynamically generated from a database on my server. I have pretty much implemented all the functionality, the only missing piece is making it capable of providing multiple quizzes. Right now I have to manually write in the code $quiz_id = 1, $quiz_title = "geography" in the quiz.php script for it to run correctly. If I can generate the values for these two variables my quiz.php would work for multiple quiz topics.

所以,这里是我的问题:我想让用户能够从一个测验题目列表中选择(在页面quizlist.php),点击他们想要采取的测验的链接,然后带他们到测验问题/选择的页面(在页面quiz.php)。此外,我想从quizlist.php发送一些值到与用户点击的特定链接相关的quiz.php。我想发送quiz_id和quiz_title到页面quiz.php,以提出正确的问题集。

So, herein lies my problem: I'd like the user to be able to choose from a list of quiz topics (on the page quizlist.php), click on the link of the quiz they want to take, and then bring them to a page with the quiz questions/choices (on the page quiz.php). Additionally, I'd like to send some values from quizlist.php to quiz.php associated with the specific link the user clicked. I'd like to send the quiz_id and quiz_title to the page quiz.php in order to present the correct set of questions.

我相信有一种方法可以使用$ _GET或$ _POST,以及使用$ _SESSION。 我的问题是哪种方式更好? 我听说$ _SESSION更安全,但我不知道我真的担心这个数据(quiz_id和quiz_title)是安全的。

I believe there is a way to do this using $_GET or $_POST, as well as using $_SESSION. My questions is which way is better? And how do I do it? I hear $_SESSION is more secure but I'm not sure if I am really worried about this data (quiz_id and quiz_title) being secure.

目前,这里是quizlist.php的代码

Currently, here is the code for quizlist.php

<?php
  // Start the session
  require_once('startsession.php');

  // Insert the page header
  $page_title = 'Quiz List';
  require_once('header.php');

  require_once('connectvars.php');

  // Make sure the user is logged in before going any further.
  if (!isset($_SESSION['user_id'])) {
    echo '<p class="login">Please <a href="login.php">log in</a> to access this page.</p>';
    exit();
  }

  // Show the navigation menu
  require_once('navmenu.php');

  // Connect to the database
  $dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);

  // Determine number of quizes based on title in quiz table
  $query = "SELECT * FROM quiz";
  $data = mysqli_query($dbc, $query);
  // Loop through quiz titles and display links for each
  while ($row = mysqli_fetch_array($data)) {
    echo '<a href="quiz.php">' . $row['title'] . '</a><br />';
  }

  mysqli_close($dbc);

  // Insert the page footer
  require_once('footer.php');
?>

感谢您的帮助!

推荐答案

使用 $ _ GET

首先,更改此行

echo '<a href="quiz.php">' . $row['title'] . '</a><br />';

如下所示:

echo '<a href="quiz.php?id='.$row['id'].'">' . $row['title'] . '</a><br />';

然后在 quiz.php 使用 $ _ GET ['id'] 作为主键在数据库中查找的适当测验。

And then in quiz.php, retrieve the appropriate quiz using $_GET['id'] as your primary key to look it up in the database.

$ _ GET 在这里是合适的'只是使用ID来确定要显示的测验。这里没有必要保密。当您提交更改数据库的表单数据时,您将使用 $ _ POST $ _ SESSION 可用于存储基本登录信息和其他在会话生命周期内必须在多个页面上持续存在的内容,例如向导表单。

$_GET is appropriate here because you're just using an ID to determine which quiz to display. There's no need for confidentiality here. You would use $_POST when you're submitting form data which alters the database. $_SESSION is useful for storing basic login info and other stuff that must persist across multiple pages for the life of the session, such as wizard forms.

这篇关于将变量从一个PHP页面发送到另一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆