Take the 2-minute tour ×

I have a button on a file test.php

<input type="button" name="btn" value="Download" onclick="alert('<?php echo $url; ?>')">

when the file is clicked, it alerts the page's url.

I have another page, test2.php.

I want the url that is being alerted to be displayed out in test2.php

How can I accomplish this?

share|improve this question
You want from one page to change another? it's impossible if I understand the question. –  gdoron Jun 26 '12 at 16:23
can you explain what exacly you want to do? i don't understand... –  Piotr Olaszewski Jun 26 '12 at 16:24
no just to send the file and output it on test2.php –  Ronny Jun 26 '12 at 16:24
You could use a form and POST that value to the next page. –  Sven Neves Jun 26 '12 at 16:24
You need to make a form on page 1 that submits to page 2. Set the value of a variable on that form to the URL and submit. Then page 2 can read the form value. –  Archer Jun 26 '12 at 16:25
show 1 more comment

closed as not a real question by gdoron, tkone, tereško, Charles, Graviton Jun 27 '12 at 14:23


3 Answers

up vote 1 down vote accepted

Depending on how important it is that it be a button, you could create a form that would send the $url in POST to test2.php. There are many workarounds, but one is the ever-elusive 'hidden' input.

The code on test.php would end up looking like:


<form action="test2.php" method="POST">
    <input type="hidden" name="url" value="<?php echo $url ?>" >
    <input type="submit" name="download" value="" onclick="alert('<?php echo $url; ?>')">

While the code on test2.php would have to include:

<?php $sent_url = $_POST['url']; 
      echo $sent_url;

Hope this helps!


P.S. More information on other ways to do this can be found in this previous question.

share|improve this answer
add comment

There are many ways to pass data:

Your value: <?php $url="http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']; ?>

Forms hidden input field (POST | GET)

<form method="POST" action="page2.php">
  <input type="hidden" name="url" value="<?php echo $url;?>">

Change method="POST" to method="GET" for GET

A Link (GET)

<a href="page2.php?url=<?php echo urlencode($url);?>">Download</a>

Through a session

session_start(); //First line of page1 and page2

Then to get the value use the globals $_POST and $_GET or $_SESSION depending on which method you choose.

Even webstorage javascript (HTML5):

<div id="result"></div>

<script type="text/javascript">
    if (localStorage.passthis){
    //Already set, perhaps set it here on page1 and and display it here on page2
    else{ //Not set
          localStorage.passthis = window.location.href;
    document.getElementById("result").innerHTML="Passed from page1.php: " + localStorage.passthis;
    //No web storage

Hope it helps, I suggest you do alittle research first before asking a question. php.net is your friend.

share|improve this answer
Thank you soo much Lawrence... can't understand why most people coud not understand my question. thanks again. –  Ronny Jun 26 '12 at 16:47
No probs... have fun! –  Loz Cherone Jun 26 '12 at 16:59
add comment

You can send data via a form GET or POST: http://www.w3schools.com/php/php_forms.asp

Example URL: http://yoursite.com/page2.php?frompageone=Hello

echo $_GET['frompageone']; // Echos "Hello"

POST is more secure than GET, this is just a quick example.

share|improve this answer
Ohhh, w3school link watch out as several pissed off users move in. :) –  gdoron Jun 26 '12 at 16:25
Okay, so I'm not posting a link to w3fools.com. –  bfavaretto Jun 26 '12 at 16:30
add comment
