简体   繁体   中英

How to pass parameters with `include`?

I have a PHP script called customers.php that is passed a parameter via the URL, eg:

http://www.mysite,com/customers.php?employeeId=1

Inside my customers.php script, I pick up the parameter thus:

$employeeId = $_GET['employeeId'];

That works just fine. I also have an HTML file with a form that inputs the parameter, and runs another php script, viz:

<form method="post" action="listcustomers.php">
  Employee ID:&nbsp; <input name="employeeId" type="text"><br>
  <input type="submit" value="Show">
</form>

Then in my listcustomers.php script, I pick up the parameter so:

$employeeId = $_POST['employeeId'];

So far so good. But his is where I am stuck. I want to run my customers.php script, and pass it the parameter that I pave picked up in the form. I am sure this is really simple, but just cannot get it to work. I have tried:

include "customers.php?employeeId=$employeeId&password=password";

But that does not work. Nor does anything else that I have tried. Please help me.

You can't add a query string (Something like ?x=yyy&vv=www) onto an include like that. Fortunately your includes have access to all the variables before them, so if $_GET['employeeId'] is defined before you call:

include 'customers.php';

Then customers.php will also have access to $_GET['employeeId'];

您的包含文件将可以直接访问GET变量。

PHP includes are really includes; they attach piece of code from that location to this location.

PaulPRO's answer most likely tells what you want to achieve, but also other variables would be usable in the included file. So if you define $foo = "bar"; in index.php and include layout.php after that line, you could call this $foo variable in that layout.php file.

If you seriously want to pass an URL to the file, you can insert the URL into variable and in included file parse it with parse_url

EDIT: and yes, I do not suggest adding query params to actual include url as it starts to mess up with GET array and eventually you'll just be confused what there should be and start smacking your head into wall.

You can pass the value through Session

Like

<?php
    $_SESSION['name']='peter';
    $_SESSION['age']=28;
    $_SESSION['name']='Peter';
    include('person_info.php');
?>

In person_info.php, start the session and put all session value in the variable and unset the session.

You can use $GLOBALS to solve this issue as well.

 $tit = "Hello";

 $GLOBALS["docTitle"] =  $tit;

 include ("my.php");

This is not so clean but this will work if you need to enter a static parameter:

if (true){
    echo "<meta http-equiv='refresh' content='0;url=query_source.php?random=red%20bull' />";
}
else
{
    echo "<meta http-equiv='refresh' content='0;url=query_source.php?random=blue%20bull' />";
}

Just put it within your source code for the page which calls the query_source.php.

Once again, it is NOT very clean...

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