views:

57

answers:

4

Hi..

I need to use include Function with variable.

but,when I try to do it I faced some errors .

Code :

$year=$_POST['year'];

$month=$_POST['month'];

$day=$_POST['day'];

include "Event.php?year=".$year."&month=".$month."&day=".$day;

so,can U help me ? : )

+4  A: 

When you include files, you can't pass them any arguments. However they DO inherit any variables in the current scope (global or method).

I'm guessing at what your code does, but I assume you can just do this:

include "Event.php";

Coronatus
thanks dear Coronatus .but ,your Suggestion it doesn't work : ( the main code I wrote it , to make a calender .therefore ,I need to do this with include to refresh every add new event in specific day .
Nina
Knowledge Craving
+3  A: 

You dont need to use that kind of sintax. The $year, $month and $day variables will be perfectly visible and accessible on the Event.php file.

To get along with what you want, check PHP manual on http://php.net/manual/en/function.include.php to see some examples.

Ronaldo Junior
A: 

Include the Event.php directly and access the $_POST or $_GET variables from there.


include('Event.php');

// Event.php:
echo $_POST['year']; // works
Alec
+1  A: 

I'm not sure what you're trying to achieve, but I have a couple of guesses handy.

1 - You are trying to redirect the user, with those parameters appended to the url, in which case you will probably need to utilise header:

header("Location: http://example.com/Event.php?year=$year&month=$month&day=$day");

2 - You are trying to capture the output of that page given the presence of certain GET parameters, in which case you can utilise file_get_contents:

$output = file_get_contents("http://localhost/Event.php?year=$year&month=$month&day=$day");
karim79
yes ,your First guesses is exactly what i want to do ,but ,it does not work with me can U tell me how ?
Nina
@Nina - what exactly happens. You *cannot* redirect *after* sending any output to the browser, so no echo statements, nothing.
karim79
Knowledge Craving