How to pass variable values in URL from page to page with PHP?

by Yang Yang on January 5, 2009

in PHP Tips & Tutorials

You can see dynamic URLs everywhere on the web with busy interactive sites. Variables and their values are passed from one page to another in this way. Or more precisely, a page served in PHP (a single file php script) can accept external input in the form of a dynamic URL such as http://www.asite.com/send.php?recipient=xx@xx.com&subject=hi&body=bye.

In the example above, when someone accesses this URL in browser, 3 variables and their values are successfully passed to the php script send.php. It then takes the input, does some basic processing and sends an email to xx@xx.com with hi as the subject and bye as the body message.

Therefore to pass variables and their values from one page to another, you can just make a static HTML link:

<a href="http://www.asite.com/send.php?recipient=xx@xx.com&subject=hi&body=bye.">send the email</a>

on the source page. After one clicking the link and navigating to the URL, the email is sent after send.php gets the 3 values.

Inside send.php we can access and use the values passed to it in the following way:

<?php
$recipient = $_GET['recipient']; // That’s it. Any variable value passed in URL can be accessible via array $_GET.
$subject = $_GET['subject'];
$body = $_GET['body'];

mail($recipient, $subject, $body); // php mailing function
?>

Aside from $_GET, $_REQUEST can also be used to access these variables.

Related Posts

Leave a Comment

Previous post:

Next post: