Using $_GET with php Include() function

Posted on: January 27th, 2012 by deanpugh

Admittedly, this probably doesn’t come up alot for people, but recently I have been creating a project management system for someone and am trying to make it as user friendly as possible using all my known tricks.

I want to use an Include function for php when a page loads, but this page doesn’t include functions because its also called by jQuery and Ajax. So I simply just want to call this page and display the output.
 

Problem

Only problem is that I send it $_GET variables via the URL when I call it using jQuery. If I try the code below, it doesn’t work, because it is actually looking for a file with the exact name, and not clever enough to work out they are extra variables I am sending it.

<?php
include('mypage.php?id=1&name=deanpugh');
?>

 

Solution

To get round this, we only call the page name in the include statement, and we pre set the $_GET arrays before.

<?php
$_GET = array();
$_GET[id] = '1';
$_GET[name] = 'deanpugh';
include('mypage.php');
?>

 

Conclusion

Although this adds more code, it’s still fairly simple enough to use without it becoming a problem. Plus, it doesn’t look that messy either, and I like code that’s still readable.

I must thank Cory Gagliardi, who added a comment in the PHP documentation which allowed me to solve my problem after over an hour of tearing my hair out.

Tags: , , , , ,

Comments are closed.