简体   繁体   English

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

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

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. 可以在另一个页面上访问该变量,以便我可以使用该变量运行mysql查询。

use the get method in the url. 在url中使用get方法。 If you want to pass over a variable called 'phone' as 0001112222: 如果要将名为“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)上,您可以通过以下方式访问此var:

$_GET['phone']

You want sessions if you have data you want to have the data held for longer than one page. 如果您希望数据保留的时间超过一页,则需要会话

$_GET for just one page. $_GET只有一页。

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

on page.php page.php

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

will output: data 将输出:数据

You can pass via GET. 你可以通过GET传递。 So if you want to pass the value foobar from PageA.php to PageB.php , call it as PageB.php?value=foobar . 因此,如果您想将PageA.php的值foobar PageA.phpPageB.php ,请将其称为PageB.php?value=foobar

In PageB.php, you can access it this way: 在PageB.php中,您可以通过以下方式访问它:

$value = $_GET['value'];

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=''很好)以避免您提到的错误。

You can use Ajax calls or $_GET["String"]; 您可以使用Ajax调用或$ _GET [“String”]; Method 方法

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. 如果您尝试直接从另一个PHP文件访问该变量,则可以使用include()include_once()包含该文件,从而可以访问该变量。 Note that this will include the entire first file in the second file. 请注意,这将包括第二个文件中的整个第一个文件。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何不使用表单将PHP页面中的变量传递到另一个页面? - How to pass variables in PHP page to another page without using form? PHP:如何在不使用表单或按钮且不使用会话变量的情况下将变量从一页传递到另一页? - PHP: How to pass variable from one page to another without a form or button, and without using session variable? 从一种形式传递php变量,以另一种形式提交给数据库 - pass php variables from one form to be submitted to database in another 如何不使用&#39;global&#39;将页面中的变量从一个php脚本传递到另一个php脚本? - How to pass variables in page from a php script to another php script without using 'global'? CakePHP 2.2.3:如何将变量从一种形式传递到另一种形式 - CakePHP 2.2.3: How to pass variables from one form to another 如何在没有PHP的情况下用PHP和Javascript将数据从一页传递到另一页? - How can I pass data from one page to another in PHP and Javascript without from? 如何将变量从 php 页面传递并放入另一个页面(表单)? - How to pass and put variable from a php page into a another page(form)? 如何在Symfony2中将变量从一个页面传递到另一个页面 - How to pass variables from one page to another in Symfony2 在不使用表单php的情况下将数据从一页发送到另一页 - Sending Data from one page to another without using form php 将会话变量从一个页面传递到另一个页面 - pass session variables from one page to another
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM