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

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?[email protected]&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 [email protected] 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?[email protected]&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.

1 thought on “How to pass variable values in URL from page to page with PHP?”

  1. Hi,

    I just like to know I need to send the html form data from my website to another database server to store the details and meanwhile the same details should mail to my email id is this using php will anyone guide me on this how to achieve the same?

Comments are closed.

Scroll to Top