简体   繁体   中英

How to pass multiple parameters in url using php

   if(isset($_POST['submit'])){
        $sem = $_POST['semester'];
        $sess = $_POST['session'];
         $sexam = $_POST['exam'];
         $_SESSION['sem'] = $sem;
         $_SESSION['sess'] = $sess;
         $_SESSION['exa'] = $sexam;}?>

       <a href="print.php?id=<?php echo 
       array($_SESSION['sem'],$_SESSION['sess'],$_SESSION['exam']);  ?>" 
        target="_blank" class="btn btn-default"><i class="fa fa-print"></i> Print</a>

How can I pass these SESSION variables in URL to another page?

A session is a way to store information (in variables) to be used across multiple pages. Unlike a cookie, the information is not stored on the users computer.

So; Session variables hold information about one single user, and are available to all pages in one application.

Start a PHP Session

Page 1:

<?php
// Start the session
session_start();
?>
<!DOCTYPE html>
<html>
<body>

<?php
// Set session variables
$_SESSION["favcolor"] = "green";
$_SESSION["favanimal"] = "cat";
echo "Session variables are set.";
?>

</body>
</html>

Page 2:

<?php
session_start();
?>
<!DOCTYPE html>
<html>
<body>

<?php
// Echo session variables that were set on previous page
echo "Favorite color is " . $_SESSION["favcolor"] . ".<br>";
echo "Favorite animal is " . $_SESSION["favanimal"] . ".";
?>

</body>
</html>

Notice that session variables are not passed individually to each new page, instead they are retrieved from the session we open at the beginning of each page session_start() .

Refer this tutorial: PHP 5 Sessions

There is no need to use $_SESSION variable

<?php
if( isset( $_POST['submit'] ) ) {
    $sem = $_POST['semester'];
    $sess = $_POST['session'];
    $sexam = $_POST['exam'];
    $_SESSION['sem'] = $sem;
    $_SESSION['sess'] = $sess;
    $_SESSION['exa'] = $sexam;

    $urlFromSession = "semester={$_SESSION['sem']}&session={$_SESSION['sess']}&exam={$_SESSION['exa']}";
    $urlFromPost = "semester={$_POST['semester']}&session={$_POST['session']}&exam={$_POST['exam']}";
}
?>

<a href="print.php?<?= $urlFromSession ?>" target="_blank" class="btn btn-default"><i class="fa fa-print"></i> Post - urlFromSession</a><br/>

<a href="print.php?<?= $urlFromPost ?>" target="_blank" class="btn btn-default"><i class="fa fa-print"></i> Post - urlFromPost</a><br/>

URL will be something like this print.php?semester=test&session=test&exam=test

You will get parameters inside print.php page

<?php
echo $_GET['semester'];
echo $_GET['session'];
echo $_GET['exam'];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM