PHP Pass variable to next page

后端 未结 8 2320
Happy的楠姐
Happy的楠姐 2020-11-21 06:40

It seems pretty simple but I can\'t find a good way to do it.

Say in the first page I create a variable

$myVariable = \"Some text\";
<
相关标签:
8条回答
  • 2020-11-21 06:52

    There are three method to pass value in php.

    • By post
    • By get
    • By making session variable

    These three method are used for different purpose.For example if we want to receive our value on next page then we can use 'post' ($_POST) method as:-

    $a=$_POST['field-name'];
    

    If we require the value of variable on more than one page than we can use session variable as:-

    $a=$_SESSION['field-name];
    

    Before using this Syntax for creating SESSION variable we first have to add this tag at the very beginning of our php page

    session_start(); 
    

    GET method are generally used to print data on same page which used to take input from user. Its syntax is as:

    $a=$_GET['field-name'];
    

    POST method are generally consume more secure than GET because when we use Get method than it can display the data in URL bar.If the data is more sensitive data like password then it can be inggeris.

    0 讨论(0)
  • 2020-11-21 06:54

    Thanks for the answers above. Here's how I did it, I hope it helps those who follow. I'm looking to pass a registration number from one page to another, hence regName and regValue:

    Create your first page, call it set_reg.php:

    <?php
    
    session_start();
    
    $_SESSION['regName'] = $regValue;
    
    ?>
    
    <form method="get" action="get_reg.php">
        <input type="text" name="regName" value="">
        <input type="submit">
    </form>
    

    Create your second page, call it get_reg.php:

    <?php
    
    session_start();
    
    $regValue = $_GET['regName'];
    
    echo "Your registration is: ".$regValue.".";
    
    ?>
    
    <p><a href="set_reg.php">Back to set_reg.php</a>
    

    Although not as comprehensive as the answer above, for my purposes this illustrates in simple fashion the relationship between the various elements.

    0 讨论(0)
  • 2020-11-21 06:57

    try this code

    using hidden field we can pass php varibale to another page

    page1.php

    <?php $myVariable = "Some text";?>
    <form method="post" action="page2.php">
     <input type="hidden" name="text" value="<?php echo $myVariable; ?>">
     <button type="submit">Submit</button>
    </form>
    

    pass php variable to hidden field value so you can access this variable into another page

    page2.php

    <?php
     $text=$_POST['text'];
     echo $text;
    ?>
    
    0 讨论(0)
  • 2020-11-21 06:59
    **page 1**
    <form action="exapmple.php?variable_name=$value" method="POST"> 
        <button>
            <input  type="hidden" name="x">
        </button>
    </form>`
    

    page 2

    if(isset($_POST['x'])) {
        $new_value=$_GET['variable_name'];
    }
    
    0 讨论(0)
  • 2020-11-21 07:09

    Passing data in the request

    You could either embed it as a hidden field in your form, or add it your forms action URL

     echo '<input type="hidden" name="myVariable" value="'.
         htmlentities($myVariable).'">';
    

    or

    echo '<form method="POST" action="Page2.php?myVariable='.
        urlencode($myVariable).'">";
    

    Note this also illustrates the use of htmlentities and urlencode when passing data around.

    Passing data in the session

    If the data doesn't need to be passed to the client side, then sessions may be more appropriate. Simply call session_start() at the start of each page, and you can get and set data into the $_SESSION array.

    Security

    Since you state your value is actually a filename, you need to be aware of the security ramifications. If the filename has arrived from the client side, assume the user has tampered with the value. Check it for validity! What happens when the user passes the path to an important system file, or a file under their control? Can your script be used to "probe" the server for files that do or do not exist?

    As you are clearly just getting started here, its worth reminding that this goes for any data which arrives in $_GET, $_POST or $_COOKIE - assume your worst enemy crafted the contents of those arrays, and code accordingly!

    0 讨论(0)
  • 2020-11-21 07:11

    Sessions would be the only good way, you could also use GET/POST but that would be potentially insecure.

    Passing data in the session If the data doesn't need to be passed to the client-side, then sessions may be more appropriate. Simply call session_start() at the start of each page, and you can get and set data into the $_SESSION array.

    Security Since you state your value is actually a filename, you need to be aware of the security ramifications. If the filename has arrived from the client-side, assume the user has tampered with the value. Check it for validity! What happens when the user passes the path to an important system file or a file under their control? Can your script be used to "probe" the server for files that do or do not exist?

    As you are clearly just getting started here, it is worth reminding that this goes for any data which arrives in $_GET, $_POST or $_COOKIE - assume your worst enemy crafted the contents of those arrays, and code accordingly!

    0 讨论(0)
提交回复
热议问题