如何在没有表单的情况下将变量从一个 php 页面传递到另一个页面?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10252418/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 21:44:38  来源:igfitidea点击:

How to pass variables from one php page to another without form?

phpvariables

提问by Arihant

I want to know how to pass a variable from one page to another in PHP without any form.

我想知道如何在没有任何形式的情况下在 PHP 中将变量从一个页面传递到另一个页面。

What I want to achieve is this:

我想要实现的是:

  1. The user clicks on a link
  2. A variable is passed which contains a string.
  3. The variable can be accessed on the other page so that I can run mysql queries using that variable.
  1. 用户点击链接
  2. 传递一个包含字符串的变量。
  3. 可以在另一个页面上访问该变量,以便我可以使用该变量运行 mysql 查询。

回答by squarephoenix

use the get method in the url. If you want to pass over a variable called 'phone' as 0001112222:

使用 url 中的 get 方法。如果要将名为“phone”的变量传递为 0001112222:

<a href='whatever.php?phone=0001112222'>click</a>

then on the next page (whatever.php) you can access this var via:

然后在下一页(whatever.php)上,您可以通过以下方式访问此变量:

$_GET['phone']

回答by Blake

You want sessionsif you have data you want to have the data held for longer than one page.

如果您有数据希望将数据保存超过一页,则需要会话

$_GETfor just one page.

$_GET仅一页。

<a href='page.php?var=data'>Data link</a>

<a href='page.php?var=data'>Data link</a>

on page.php

page.php

<?php
echo $_GET['var'];
?>

will output: data

将输出:数据

回答by xbonez

You can pass via GET. So if you want to pass the value foobarfrom PageA.phpto PageB.php, call it as PageB.php?value=foobar.

您可以通过 GET 传递。所以,如果你想通过值foobarPageA.phpPageB.php,因为叫它PageB.php?value=foobar

In PageB.php, you can access it this way:

在 PageB.php 中,您可以通过以下方式访问它:

$value = $_GET['value'];

回答by boblangdon

check to make sure the variable is set. Then clean it before using it:

检查以确保设置了变量。然后在使用前清洁它:

isset($_GET['var'])?$var=mysql_escape_string($_GET['var']):$var='SomeDefaualtValue';

Otherwise, assign it a default value ($var=''is fine) to avoid the error you mentioned.

否则,为其分配一个默认值($var=''很好)以避免您提到的错误。

回答by boblangdon

You can use Ajax calls or $_GET["String"]; Method

您可以使用 Ajax 调用或 $_GET["String"]; 方法

回答by Isaac Marquina Koebner

If you are trying to access the variable from another PHP file directly, you can include that file with include()or include_once(), giving you access to that variable. Note that this will include the entire first filein the second file.

如果您尝试直接从另一个 PHP 文件访问该变量,则可以使用include()或包含该文件include_once(),从而可以访问该变量。请注意,这将在第二个文件中包含整个第一个文件。