-3

I have a button on a file test.php

<?php
$url="http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
?>
<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?

Ronny K
  • 3,641
  • 4
  • 33
  • 43

3 Answers3

4

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;?>">
  <p><input type="submit" value="Submit"></p>
</form>

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

<?php 
session_start(); //First line of page1 and page2
$_SESSION['url']=$url;
?>

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(typeof(Storage)!=="undefined")
{
    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;
}else{
    //No web storage
}
</script>

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

Lawrence Cherone
  • 46,049
  • 7
  • 62
  • 106
  • Thank you soo much Lawrence... can't understand why most people coud not understand my question. thanks again. – Ronny K Jun 26 '12 at 16:47
1

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.

MultiDev
  • 10,389
  • 24
  • 81
  • 148
1

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:

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

<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; ?>')">
</form>

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

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

Hope this helps!

Mason

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

Community
  • 1
  • 1
MasonWinsauer
  • 673
  • 3
  • 14
  • 32