My question is pertaining to extracting information from the URL using PHP.
I've three pages in the website I am building
catalogues.html
form_userdata.php
mail.php
The schema is as follows. The user will choose a file to download from catalogues.html
for eg
a href="form_userdata.php?id=1026&name=Vibrating Feeder - Heavy duty -VFH" target="_blank"
This id & name will be passed to form_userdata.php and after entering the details in form_userdata.php page, the page passes control to mail.php, that will check if the fields are all true and valid.
My question is how can I use the ID & name specified in "a href" in my code?
I am passing the ID and name from catalogues->form_userdata and collecting it in mail.php
Thank you for your valuable input
you can use sessions to store it when processing form_userdata.php and then use it in mail.php. That way you don't have to pass it in every URL.
when I add $id=$_POST['id']; at
form_userdata, I get an error saying
"undefined index: id"
That's quite logical, since you're passing them in the URI, and thus have to read them using $_GET in PHP, ie. do :
$id = isset($_GET['id']) ? (int)$_GET['id'] : null;
I found this solution:
session_start();
$_SESSION['id'] = $id=$_GET['id'];
$_SESSION['name'] = $name=$_GET['name'];
this will pass the value of ID and Name to the next page.
Thank you for your help
you may use $_GET to collect and pass variables
You can access the named values in an URL (query string, query-info part) by using the $_GET superglobal. It will contain the value by it's name:
$_GET['id']
More information is in the the PHP manual: $_GET. That page is already pretty specific, I suggest you read this manual page as well: Variables From External Sources it more broadly describes how this works.
As input is very important in programming, it's really recommended to understand how it works so you can use the language for your needs more easily.
Related
I have a php page (running locally):
http://localhost/webpagefolder/home.php
When a different page is clickedon the URL changes to reflect it. So it now becomes:
http://localhost/webpagefolder/home.php?here=delivery
because I have selected the delivery page.
How would I store the part after the = in a variable?
Something like $name = delivery (or whatever is displayed after the =)
Thanks in advance
In PHP all the GET-variables are stored in the $_GET super global. In your example $_GET['here'] should contain 'delivery'.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
passing variable between pages
I need to create a variable that I can use in one PHP page to assign a category to this variable. When I've assigned a value, how I can pass that value to another PHP page?
Do I have to use a global variable?
You can pass variables from page to page using:
Sessions.
A common config file that is included throughout all of your pages.
Forms (hidden fields etc).
Cookies.
Each approach has its own use, so you'll need to choose according to the circumstance. If the variable in question will stay the same throughout your app, I'd suggest placing it in a config file. If its a user-specific variable, I'd suggest using sessions.
Its depend on you what you want to use for passing value from one php page to another.
You can use Session, Cookie or Forms by using $_GET and $_POST method:
Example : By using Session
//page1.php
session_start();
$_SESSION['name'] = $name;
//you can access this in page2.php
session_start();
echo $_SESSION['name'];
Example : By using Cookies
//page1.php
setcookie(name, value, expire, path, domain);
//you can access it in page2.php like this
echo $_COOKIE["name"];
Example : By using GET and POST
You can use GET with anchor tag and for POST you need form submission.
Read more about GET and POST :
http://php.net/manual/en/reserved.variables.post.php
http://php.net/manual/en/reserved.variables.get.php
Hope this information will help you
I'm kind of a noob at this stuff.
But I've been browsing around and I see sites that are kind alike this
www.store.com/product.php?id=123
this is really cool. but How do I do it?
Im stuck using something like this
www.store.com/product/product123.php
If you could tell me how I can go about do this it would be awesome!
What you're looking at is a $_GET argument.
In your PHP code, try writing something like this:
$value = $_GET['foo'];
Then open your page like this:
hello.php?foo=123
This will set $value to 123.
You need to use the $_GET here.
if you use the following:
?id=123
then this will be how to use it and the result
$_GET['id'] (returns the 123)
You can use as many $_GET arguments as you need, for example:
?id=123&foo=bar&type=product
$_GET is an array of what parameters are in the url, so you use it the same way as an array.
Create a file called product.php with this code:
<?php
echo "The argument you passed was: " . $_GET['id'];
?>
Now run this URL in your browser:
http://<yourdomain>/product.php?id=123
and you will understand how $_GET works.
Those are called URL parameters (what they're contained in is called a query string), and they're not unique to PHP but can be accessed in PHP using the $_GET superglobal.
Similarly, you can get POST parameters using the $_POST superglobal, though in POST requests, these parameters are not appended to the URL.
Note: Generally, for usability purposes (and thus also SEO purposes), you want to avoid using query strings as much as possible. These days, the standard practice is to use URL rewriting to display friendly URLs to the user. So your application might accept a URL like:
/products.php?id=32
But the user only sees:
/product/32
You can do this by using mod_rewrite or similar URL rewriting capabilities to turn the friendly URL into the former query string URL internally, without having the user type out the query string.
You might want to have a look at the documentation at www.php.net, especially these pages: http://www.php.net/manual/en/reserved.variables.php
Specifically, have a look at $_GET and $_POST, which are two frequently used ways to transmit information from a browser to the server. (In short, GET-parameters are specified in the URL, as in your question, while POST-parameters are "hidden from view", but can contain more data - typically the contents of forms etc, such as the textbox you posted your question in).
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
PHP Pass variable to next page
Here is my current code:
$search = $_POST['s'];
$search = strtoupper($search);
$search = strip_tags($search);
$search = trim($search);
$search = mysql_real_escape_string($search);
I need to be able to carry on the $search variable to my second, third, etc, pages.
I'm a beginner in php and i'm sort of stuck here
It would appear that sessions are your friend here. In the simplest form, sessions will just put data in cookies that are sent to and from the user's browser. Make sure you call session_start() before you do anything with the session, this will start or resume the user's sessions. After that, you can use $_SESSION as a global associative array that will persist between pages.
Xander has already linked you to the docs, Here are some simple examples. Make sure you understand session_start() otherwise you'll have some bugs.
N.B. Do not use this basic session format for sensitive data. Look into using something like memcache to store the data and simply put the memcache key into $_SESSION. Also, consider encrypting the sessions. Those are more advanced things you should think about when dealing with user authentication/login
Assuming it is a search string, there is only sane method:
First, change the form's method to GET
Next, just pass your search variable in the query string using GET method.
The only modification you have to apply is urlencode()
So, the code should be
$query_string = 'search='.urlencode($_GET['search']);
echo "<a href='?page=2&$query_string'>page 2</a>";
producing an HTML code
page 2
so a user can click this link and you will have your search string back
While $_SESSION has been suggested, another option is to use a hidden field (with the same name and filled with the appropriate value) on subsequent generated pages. Then, when those pages are posted back, they too will have the field available in $_POSTS (this time supplied by the hidden field, not the original text field).
Advantages:
"Bound to the current page"; really good for some page context-sensitive stuff! (The session is scoped to the browser, not the page.)
Avoids the need for session/cookies (which is a non-issue if the session is already required for other purposes).
Disadvantages:
"Bound to the current page": value will be lost when navigated away from outside of back/next context. (As Bert notes, a slight modification can use this "breadcrumb" approach to alter the URL and use GET parameters, which can make the data universally persistent, at the expense of a "less pretty" URL.)
Data must be treated as untrusted and insecure, just like the original post.
Requires population of additional [hidden] fields.
Happy coding.
Use session_start() in each of the pages you want to access the search varaible
in the first page
$search = $_POST['s'];
$search = strtoupper($search);
$search = strip_tags($search);
$search = trim($search);
$search = mysql_real_escape_string($search);
set a session variable as
$_SESSION['searchStr']=$search
then in everyother page
session_start(); // at the very begining
if(isset($_SESSION['searchStr'])) {
$search=$_SESSION['searchStr']
}
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!