How to pass a variable to another page

index.php

$typesql = $_GET['type']

      

fetch_pages.php

$results = $mysqli->prepare("SELECT name, type FROM artists WHERE type = ?);

$results->bind_param("s", $typesql);
$results->execute();
$results->bind_result($name, $type);

      

I am using the above to get the artist type in index.php, I want to pass it to use it in fetch.php and bind it to my sql query.

+3


source to share


3 answers


You can put $ _GET value in session and you can use this session variable to access data in another page

Example
page 1

session_start();
$_SESSION['type']=$_GET['type'];

      



page 2

session_start();
$type = $_SESSION['type'];

      

+2


source


Data can be transferred to the next page in various ways. You can pass a variable to the next page in the following ways.

  • Session
  • Cookie
  • Url variable

But in your case, using session is the right way to go.

index.php



session_start(); 
$typesql = $_GET["type"];
$_SESSION["typesql"] = $typesql; 

      

fetch_pages.php

session_start();
$typesql = $_SESSION["typesql"]; 

$results = $mysqli->prepare("SELECT name, type FROM artists WHERE type = ?);

$results->bind_param("s", $typesql);
$results->execute();
$results->bind_result($name, $type);

      

+2


source


You can pass any parameters via url request (but URL length is limited).

IN index.php

<a href="/fetch_pages.php?type=<?=$typesql?>">fetch pages</a>

      

IN fetch pages.php

// get type from url
$typesql = $_GET['type'];
// and then bind it to sql.

      

0


source







All Articles