views:

282

answers:

3

Hello, I am trying to dynamically get a php variable and place them into my javascript variable as seen below:

var site_url = "getsite.php?name=SiteName&link=linkURL";

That above script is what I have now and it is harcoded(SiteName,linkURL).

I want to change 'SiteName' and 'linkURL' to display whatever the PHP variable is on another page.

Any help would be great, Thanks.

A: 

How is the user arriving at this page from the one with the PHP variables you want to access?

The simplest way would be to store them in the session, like so:

previous page:

<?php
session_start();
$_SESSION['siteName'] = 'mySiteName';
$_SESSION['linkURL'] = 'http://somesite.com';
?>

then, on the current page:

<?php
$name = $_SESSION['siteName'];
$url = $_SESSION['linkURL'];
?>

<script type="text/javascript">
    var site_url = "getsite.php?name=<?php echo $name; ?>&link=<?php echo $linkURL; ?>";
</script>

That should work for you - good luck!

inkedmn
Thanks, That worked.
Spyderfusion02
A: 

you will need to send thru and AJAX request via XMLHttpRequest etc to load a page like that in JS. however i am not sure if i understand that logic of that flow correctly

Sabeen Malik
+1  A: 

You can try this method: http://www.netlobo.com/url%5Fquery%5Fstring%5Fjavascript.html

function gup( name )
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
}
lod3n