Jump to content

Recommended Posts

Hi guys,

I have a Ajax request like below, However i want it to send a PHP var .

How can i do this?

 

Below is a quick thing showing the php var.

Further down i sthe Javascript var i want to populate with the php value.

 

var siteid = ( THIS IS WHERE I WANT TO INSERT THE PHP SESSION VALUE )

 

How do i do this?

 

Ive tried doing inline php but that seems to fail , Im assuming because its between script tags..

 

Humm i suppose i could use php to echo the entire script with teh values included?

Whats the *best way to do this?

 

<?
$_SESSION['siteid'] = "1234";
?>

<script language="javascript" type="text/javascript">
<!--
function getdata() {


  var ajaxRequest;
  try {
    // Opera 8.0+, Firefox, Safari
    ajaxRequest = new XMLHttpRequest();
  } catch (e) {
    // Internet Explorer Browsers
    try {
      ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try {
        ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (e) {
        // Something went wrong
        alert("Your browser broke!");
        return false;
      }
    }
  }
  // Create a function that will receive data sent from the server
  ajaxRequest.onreadystatechange = function() {
    if(ajaxRequest.readyState == 4) {
      var ajaxDisplay = document.getElementById('sessions');
      var result = ajaxRequest.responseText;
          ajaxDisplay.innerHTML = result;
    }
  }

        var nocache = Math.random();

var siteid = ( THIS IS WHERE I WANT TO INSERT THE PHP SESSION VALUE )

        var queryString = "?siteid=" + siteid ;

        ajaxRequest.open("GET", "agetdata.php" + queryString, true);

	 ajaxRequest.send(null);



}

//-->
</script>

 

 

 

 

Thanks

G

Link to comment
https://forums.phpfreaks.com/topic/160045-put-a-_session-var-in-javascript/
Share on other sites

Yeah i tried:

 

	 
        var nocache = Math.random();
	var siteid = "<?php echo $_SESSION['siteid']; ?>";
        var queryString = "?siteid=" + siteid;

        ajaxRequest.open("GET", "agetdata.php" + queryString, true);

	 ajaxRequest.send(null);

 

But this doesnt work.

Nothing happens when the JS is run.

However if i put the whole lot in PHP and echo the script with the bits i want it works.

 

Looking at the source , BOTH html outputs are the same.

even doing:

 

      var nocache = Math.random();
	<?php
	echo 'var siteid = "'.$_SESSION['siteid'].'";';
	?>

        var queryString = "?siteid=" + siteid;

 

Doesnt work.

 

Heres a copy of the actualy output into the html source:

 


        var nocache = Math.random();
	var siteid = "1234567890";
        var queryString = "?siteid=" + siteid;

Humm,

Ive given up and decided to just echo the entire javascript function with the bits i want in it.

 

This seems to work.

 

Doesnt look like it likes having <?php ?> inside of the <script> tags.

 

So i just echo the entire script built with teh vars included.

Does the file have a PHP extension on it?

 

that would be a yes, of course, because he does in fact see the siteid in the source code. it's just not passing through the rest of the JS function properly.

 

lynx, have you tried alert()ing the value of the querystring to see if it contains what it should? i know you've given up, but we might as well try and sort this out because there's no use forcing PHP to echo it all if you don't have to.

hat would be a yes, of course, because he does in fact see the siteid in the source code. it's just not passing through the rest of the JS function properly.

 

lynx, have you tried alert()ing the value of the querystring to see if it contains what it should? i know you've given up, but we might as well try and sort this out because there's no use forcing PHP to echo it all if you don't have to.

 

 

Ill give that a try .

Sorry about that. So in what way is it not working. Does the AJAX work if you just inline the value "1234567890" into siteid instead of using PHP like so -

var siteid = "1234567890";

 

Does the AJAX work? If not, maybe it's your agetdata.php that you should be looking at.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.