9

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

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.

Sebastian Hoitz
  • 9,343
  • 13
  • 61
  • 77
Philip Sheard
  • 5,789
  • 5
  • 27
  • 42
  • 2
    possible duplicate of [PHP - include a php file and also send query parameters](http://stackoverflow.com/questions/1232097/php-include-a-php-file-and-also-send-query-parameters) – Felix Kling Jul 24 '11 at 09:18
  • Well You want to give the customers.php the parameters from the form. The form will send you everything via a post, so everything is available by $_POST, however, you have got that so far and you know what is happening. So the next thing is, that you want to open the customers.php file and give the parameters from the form, what would be the employeeId. What do you want to do there in your customers.php? If you just want to call a function in that file, you could just include(customers.php) and then call the function and give the id as a paremeter to the function? – Richard Jul 24 '11 at 09:20
  • possible duplicate of [How to pass parameters to PHP template rendered with 'include'?](http://stackoverflow.com/questions/1312300/how-to-pass-parameters-to-php-template-rendered-with-include), [PHP include() with GET attributes (include file.php?q=1)](http://stackoverflow.com/questions/5675550/php-include-with-get-attributes-include-file-phpq-1), [PHP - Best Practice for Passing Variables to Include Files](http://stackoverflow.com/questions/4940154/php-best-practice-for-passing-variables-to-include-files), ... – outis Jul 24 '11 at 09:25
  • ... [Passing parameter while including PHP script](http://stackoverflow.com/questions/5509929/passing-parameter-while-including-php-script), [PHP include with HTTP get paramters](http://stackoverflow.com/questions/4987990/php-include-with-http-get-paramters), [how can I use include() Function which parameter is variable?](http://stackoverflow.com/questions/2877851/in-php-how-can-i-use-include-function-which-parameter-is-variable), [Pass value to an include file in php.](http://stackoverflow.com/questions/2644199/pass-value-to-an-include-file-in-php), ... – outis Jul 24 '11 at 09:30
  • [How to pass arguments to an included file?](http://stackoverflow.com/questions/4315271/how-to-pass-arguments-to-an-included-file). See also [Why can't I pass variables into an included file in PHP?](http://stackoverflow.com/questions/1897243/why-cant-i-pass-variables-into-an-included-file-in-php) – outis Jul 24 '11 at 09:37

6 Answers6

21

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'];

Paul
  • 139,544
  • 27
  • 275
  • 264
  • 4
    "You can't add a query string ..." - Actually you can, but it's not recommended: http://php.net/manual/en/function.include.php#example-128 Anyway, +1 for useful answer – Dor Jul 24 '11 at 09:20
  • 4
    What I did not realise is that I can just set elements of $_GET[]. I has assumed that it would be read-only. – Philip Sheard Jul 24 '11 at 09:28
2

Your include file will have access to the GET variables directly.

outis
  • 75,655
  • 22
  • 151
  • 221
Prashant Singh
  • 3,725
  • 12
  • 62
  • 106
2

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.

Smar
  • 8,109
  • 3
  • 36
  • 48
0

You can pass the value through Session

Like

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

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

SUGU
  • 407
  • 1
  • 5
  • 8
0

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

 $tit = "Hello";

 $GLOBALS["docTitle"] =  $tit;

 include ("my.php");
Du-Lacoste
  • 11,530
  • 2
  • 71
  • 51
-2

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...

Pang
  • 9,564
  • 146
  • 81
  • 122