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

前端 未结 6 2370
滥情空心
滥情空心 2020-12-13 07:37

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
相关标签:
6条回答
  • 2020-12-13 07:47

    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'];
    
    0 讨论(0)
  • 2020-12-13 07:51

    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. Note that this will include the entire first file in the second file.

    0 讨论(0)
  • 2020-12-13 08:02

    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.

    0 讨论(0)
  • 2020-12-13 08:04

    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

    0 讨论(0)
  • 2020-12-13 08:09

    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']
    
    0 讨论(0)
  • 2020-12-13 08:12

    You can use Ajax calls or $_GET["String"]; Method

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