如何在没有形式的情况下传递变量 [英] How to pass variable through without form

查看:13
本文介绍了如何在没有形式的情况下传递变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试维护一个自定义构建的 PHP 名册系统.我正在尝试将一个变量传递给第二个 PHP 页面.以前,这是通过带有下拉菜单的表单完成的:

I am trying to maintain a custom built PHP roster system. I'm trying to pass a variable along to a second PHP page. Previously, this was being done through a form with a dropdown:

<form METHOD=POST ACTION=\"awardedit.php\">
<input TYPE=\"hidden\" NAME=\"pagestep\" value=\"1\">
<select name=\"username\">
<option value=\"" . $username . "\">" . $username;
</select>
</form>

(不知何故,即使没有关闭选项标签,这仍然有效)

(somehow, even without closing the option tag, this still worked)

这些后面是单独的按钮,决定了它的去向"awardedit.php"

These were followed by individual buttons that determined where it went to on "awardedit.php"

但是,当我们拥有大量用户时,下拉列表并不是要走的路.可用性不是很好.

However, the dropdown list is not the way to go when we have a large number of users. The usability is not great.

我正在尝试查询数据库以获取用户列表,然后在表格中显示用户,以及那些让您进入 Awardedit.php 不同部分的按钮.

I'm attempting to query the database to get a list of the users, then display the users in a table, along with the those buttons that get you to different sections of awardedit.php.

如果没有form method = post action = Awardedit.php",我就是不知道如何传递 $username 变量

I just cannot figure how to pass the $username variable through without "form method = post action = awardedit.php"

我知道如何通过回显用户名来显示用户名,但我不知道传递变量的代码.

I know how to echo the username through to show the users username, but I don't know the code to pass through the variable.

对不起,这太糟糕了,但我真的很业余,一直在阅读和阅读以试图解决这个问题,(会话,ajax,$_POST),但我就是不知道如何实施此更改.

I'm sorry this is so bad, but I really am very amateur and have been reading and reading to try and figure this out, (sessions, ajax, $_POST), but I just can't figure out how to implement this change.

推荐答案

以下是针对您的问题的以下解决方案:

Here are the following solutions to your problem :

使用 GET 将变量传递给您的另一个页面:

Use GET to pass variable to your another page :

这里是:

<a href="/awardedit.php?username=<?php echo $username; ?>" >Submit Username</a>

注意:现在当您点击它时它会变成一个链接,因此它会将您的username 变量数据传递到您的awardedit.php 页面.

Note : Now it becomes a link when you click on it so it passes your username variable data to your awardedit.php page.

然后在该页面上,您可以使用以下代码捕获变量值:

And then on that page you can catch the variable value using the following code:

<?php $username = $_GET["username"];
//And then do whatever you want to do with it
?>

如果你想这样做,那么你可以使用以下方法来做到这一点:

If you want to do it this way so then there you can use the following way to do this :

如果您不想做任何不安全的事情,请使用 COOKIES :

Use COOKIES if you don't want to do anything unsecure :

setcookie("username", $username, time()+5);
header("Location: awardedit.php");

然后在您的 awardedit.php 页面获取 cookie 为:

And then fetch the cookie at your awardedit.php page as :

if (isset($_COOKIE['username'])) {
$username = $_COOKIE['username'];
}

如果您不想这样做,那么您可以通过以下方式进行操作,这是更安全的方式:

Still if you don't want to do it this way so you can do it the following way and it's more secure way :

像这样设置会话:

$_SESSION['username'] = $username;

并在您的 awardedit.php 页面上获取值.您可以这样做:

And on your awardedit.php page to fetch the value.you can do it like this way :

$username = $_SESSION['username'];
echo $username;

记住在尝试访问 $_SESSION 数组之前,以及在将任何输出发送到浏览器之前,在这两个页面上运行 session_start() 语句.

Remember to run the session_start() statement on both these pages before you try to access the $_SESSION array, and also before any output is sent to the browser.

这篇关于如何在没有形式的情况下传递变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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