0

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.

2
  • Do you mean you would like to pull the URL Parameters (name=SiteName) and re-use this within your JavaScript? Commented Sep 29, 2009 at 20:09
  • echo "var site_url = \"getsite.php?name=", sitename, "&link=", linkURL, "\";" ? Commented Sep 29, 2009 at 20:09

3 Answers 3

1

You can try this method: http://www.netlobo.com/url_query_string_javascript.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]; } 
Sign up to request clarification or add additional context in comments.

Comments

0

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!

Comments

0

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.