Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

variables

php

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

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.
like image 990
Arihant Avatar asked Apr 20 '12 19:04

Arihant


People also ask

How do I pass a variable from one page to another in PHP?

$myVariable = "Some text"; And the form's action for that page is "Page2. php".

How do you pass a variable to another page?

There are two ways to pass variables between web pages. The first method is to use sessionStorage, or localStorage. The second method is to use a query string with the URL.

How many ways we can pass the variable through the navigation between the pages in PHP?

Three ways can pass the variable through the navigation between the pages: Put the variable into session in the first page, and get it back from session in the next page.


3 Answers

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

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

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

$_GET['phone']
like image 132
squarephoenix Avatar answered Oct 05 '22 15:10

squarephoenix


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

$_GET for just one page.

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

on page.php

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

will output: data

like image 38
Blake Avatar answered Oct 05 '22 17:10

Blake


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

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

$value = $_GET['value'];
like image 20
xbonez Avatar answered Oct 05 '22 15:10

xbonez