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 ? : )
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";
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.
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");
Include the Event.php directly and access the $_POST or $_GET variables from there.
include('Event.php');
// Event.php:
echo $_POST['year']; // works
Related
I tried this :
in page1.php :
$GLOBALS['nb_ajout_client'] = "rrrr";
in page2.php :
$GLOBALS['nb_ajout_client'] .= " kkkkk";
I displayed the value of $GLOBALS['nb_ajout_client'] but it is kkkkk.
So how to create a global variable and use it anywhere ?
Global variables only survive the lifetime of the program execution.
Each time you load a (PHP) page, you run a program from scratch. If you load a different page then you run a different program.
If you want to store data between them then you need to actually store it somewhere and then read it back from there in the other program.
If you want to do this globally, then the usual approach is to use a database.
If you want to do this on a per-user basis, then the usual approach is to use a session.
You could also pass the data via the browser (e.g. by putting it in the query string of a link and then reading it back from $_GET).
A variable is only 'global' in the current script. If you want to use a variable from page1, you need to include it from page2.
To pass variables to other page (other request) use PHP Sessions
you can try this.
g1.php
<?php
$GLOBALS['nb_ajout_client'] = "rrrr";
g2.php
<?php
include('g1.php');
$GLOBALS['nb_ajout_client'] .= " kkkkk";
var_dump($GLOBALS['nb_ajout_client']);
$GLOBALS are global in all scopes throughout a script.
To pass the value to another page:
use $_POST
use Session (server-side )
use cookies(client-side)
So I've search far and wide to try and find an example of this but found nothing. It seems like a simple thing however I continue to get errors.
Essentially what I've got is:
<?php
$articleCount = 3;
include('/newsArticles.php?id=$articleCount');
?>
It seems fairly self explanatory. What I need is an include that I can pass a value on into the file which I can then grab with $_GET['id'].
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['id'] is defined before you call:
include('/newsArticles.php');
Then newsArticles.php will also have access to $_GET['id'];
You don't need to pass a $_GET variable. Just set it, and reference it in your included file.
Your included file will have access to $articleCount without any additional work needed.
Try this:
$_GET['id'] = 3;
include('/newsArticles.php');
Since you are including a script it would seem as though you have access to the script itself. Even if you don't have access to the script you can edit the $_GET variable from within the script you showed above, like this:
<?php
$_GET['id'] = 3; // or $_GET['id'] = $articleCount;
include('/newsArticles.php');
?>
That's because the script newsArticles.php has the same access to the $_GET variable, unless the script was specifically created so that it extracts the variables from the URL.
Try it.
I'm still kinda new to HTML/PHP and wanted to figure out how to streamline my pages a little bit more. I want to try to pass a variable from the page I include another PHP file on.
For example:
<?php include "quotes.php"; $name='tory'?>
I then want to use this variable name, $name='tory', in my quotes.php file. I'm unsure if I'm going about this the correct way because if I try to use the variable $name in my quotes.php file, it says that "name" is undefined.
Question has been answered. Needed to switch the statements. Thank you all!
Assign $name variable before including other files:
<?php
$name='tory';
include "quotes.php";
?>
Reverse it:
<?php $name='tory'; include "quotes.php"; ?>
You cannot use a variable before it was declared.
In your example, you're including quotes.php before the $name variable declaration, it will never work.
You can do
<?php $name='tory'; include "quotes.php"; ?>
Now, $name exists in "quotes.php"
What you're doing isn't necessarily the best way to go about it, but to solve your specific problem, simply define the variable before including the file. Then the variable will be globally scoped and available in the include file.
<?php
$name = 'tory';
include "quotes.php";
?>
You need to define $name first before including quotes.php.
You have to declare the variable $name before including the file.
<?php
$name = 'tory';
include 'quotes.php';
?>
This makes sense because the file you included will get parsed and executed and then move on with the rest.
The statements are executed in sequence. It is as though you had copy-and-pasted the contents of quotes.php at the point in your script where the include statement is found. So at the point where you execute quotes.php, the statement defining $name has not happened yet. Therefore to get the behaviour you want, you should reverse the order of the two statements.
On one of my pages I have a require_once('../path/to/url/page.php'); which works with no problems. The moment I add a query string require_once('../path/to/url/page.php?var=test'); it won't include the file anymore. It's just blank. Anyone have any ideas of why? Can you not use a query-string in a require?
Thanks,
Ryan
By using require_once('../path/to/url/page.php?var=test');, php will not make a new request to page.php, it will actually search for the file named page.php?var=test and include it, because in unix, you are allowed to have such a filename. If you want to pass a variable to that script, just define it: $var="test" and it will be available for use in that script.
require loads a File (from a file path) to include. It does not request that file through apache (or other webserver), therefore you cannot pass query strings in this way.
If you need to pass data into the file, you can simply define a standard php variable.
Example
<?php $a_variable = "data"; require_once('../path/to/url/page.php'); ?>
Note, the variable must be set before the include/require is called, otherwise it won't be available.
All answes true. But most importantly: since $_GET is a global, it's present' in all included files as well, so there's absolutely no use in passing those parameters with the include.
require only accepts paths it would be pointless to add any request since it doesn't make any
it simple includes the required code into the current one
How do I make it so that I can make a thing at the end of the address where the .php is and then tell it to do certain things. For example pull up a page like this:
sampardee.com/index.php?page=whatever
Help?
Anything else I could do with this?
This is generally achieved with the global php array $_GET. You can use it as an associative array to 'get' whatever variable you name in the url. For example your url above:
//this gives the $page variable the value 'whatever'
$page = $_GET['page'];
if($page == 'whatever'){
//do whatever
}
elseif($page == 'somethingelse'){
//do something else
}
Check out the php documentation for more information:
$_GET documentation
and there's a tutorial here:
Tutorial using QUERY_STRING and _GET
A small improvement over Brett's code:
if (array_key_exists('page', $_GET) === false)
{
$_GET['page'] = 'defaultPage';
}
$page = $_GET['page'];
// ... Brett Bender's code here
$_GET is usually used if you are sending the information to another page using the URL.
$_POST is usually used if you are sending the information from a form.
If you ever need to write your code so that it can accept information sent using both methods, you can use $_REQUEST. Make sure you check what information is being sent though, especially if you are using it with a database.
From your question it looks like you are using this to display different content on the page?
Perhaps you want to use something like a switch to allow only certain page names to be used?
i.e.
$pageName=$_REQUEST['page'];
switch($pageName){
case 'home':$include='home.php';break;
case 'about':$include='about.php';break;
case default:$include='error.php';break;
}
include($include);
This is a really simplified example, but unless the $page variable is either home or about, the website will display an error page.
Hope it helps!
I'm not quite sure what you're asking, but I think you're asking how to use GET requests.
Make GET requests against any PHP page as follows:
www.mysite.com/page.php?key1=value1&key2=value2
Now, from within PHP, you'll be able to see key1 -> value1, key2 -> value2.
Access the GET hash from within PHP as follows:
$myVal1 = $_GET['key1'] #resolves to "value1"
$myVal2 = $_GET['key2'] #resolves to "value2"
From here, play with your GET variables as you see fit.
The system of adding page parameters to a URL is know as HTTP GET (as distinct from HTTP POST, and some others less commonly used).
Take a look at this W3 schools page about GET in PHP and ahve a play about in getting parameters and using them in your PHP code.
Have fun!