Juicy
Juicy

Reputation: 12530

Include page with variables

I'm fairly new to PHP. I have learned how to use "include" to include HTML and PHP files into my PHP page. However I'm trying to include a PHP page and pass variables to it at the same time. My initial try was this:

include 'localhost/blah/test.php?var1=20&var2=30';

But that gives me this error:

Warning: include(...): failed to open stream: No such file or directory in C:\xampp\htdocs\blah\index.php on line 78

I'm guessing that include only allows me to open a file, and not a URL. I've done some search on google and found that if allow_url_fopen is ON in my php.ini file then I should be able to do this. I checked the php.ini and it is indeed set to on.

EDIT FOLLOWING ANSWERS AND COMMENTS:

Then it seems what I want to do will not work in this way. Eventually this is what I would like:

On index.php, when user clicks on LINK_A, reload only #DIV_FOO with blah.php?var=A.

If user clicks LINK_B, reload only #DIV_FOO with blah.php?var=B.

My page is massive and I'm trying to limit the load by only having to request a "subpage" at a time, instead of requesting the whole big index.php. How would you suggest doing that?

To add to my previous comment in a bit clearer: index.php will include blah.php. If user clicks a link, I don't want index.php to be reloaded, but only blah.php changed to blah.php?var=A. Hope you understand me...

Upvotes: 0

Views: 90

Answers (4)

Logvinov Alecksey
Logvinov Alecksey

Reputation: 101

Then you include some file to current script all variables will such as in base script. Remeber, then you include files and working with object variables, this will not works.

Upvotes: 0

user1864610
user1864610

Reputation:

Even if you can include external files by URL you still won't be able to pass variables to it as part of the URL - PHP doesn't execute the code it includes at inclusion time - just when the program flow gets to it.

However, there's no need. Included code receives the scope in which it is included, so you can declare your variables, then include your code and your included code will have access to the declared variables:

main.php

<?php
$a = 5;
$b = 10;
include("extra.php");
?>

extra.php

<?php
$c = $a * $b;
echo $c;       // 50
?>

Upvotes: 2

user557846
user557846

Reputation:

no need to parse the variables, just define as 'normal

$var1=20;
$var2=30;

include '/PATH/test.php';

and don't parse an external file if its hosted locally

Upvotes: 1

developerwjk
developerwjk

Reputation: 8659

To do that you have to include fully qualified domain name

http://php.net/manual/en/function.include.php

 // Won't work; looks for a file named 'file.php?foo=1&bar=2' on the
 // local filesystem.
 include 'file.php?foo=1&bar=2';

 // Works.
 include 'http://www.example.com/file.php?foo=1&bar=2';

Upvotes: 0

Related Questions