-1

There's probably something pretty easy here that I'm missing but I can't seem to figure it out.

I'm trying to pass a PHP variable "$session" from one PHP page to another PHP page using GET. The GET shows in the new URL but the value is not passed to the page so that I can retrieve it. Below is the relevant code.

On the main Index.php page.

<div class="mapgroup">
  <table>
    <tr>
       <td></td>
       <td id="refreshKML" ></td>
       <?php echo "<td><a href='3D-Earth.php?session=$session' title='Full Size Google Earth' target='_blank' >Click here</a> for full size Google Earth</td>"; ?>
    </tr>
  </table>
  <div id="map3d"></div>
</div>

The "$session" variable is passed to 3D-Earth.php using GET and the correct value shows in the URL. However, when I try to retrieve the variable with:

<?php
  $session = $_GET["session"];
?>

and then try to create a javascript variable:

<script>
  var session = <?php echo $session; ?>;
</script>

that I will use to concatenate the below string:

var href = 'http://localhost/satellites/' + session + '/master.kml';

nothing is being passed.

I appreciate any help anyone can provide. Thanks in advance.

Additionally, I've inserted the below code at the bottom of my 3D-Earth.php page to verify that the GET is being passed.

  <div>
    <table><p style='color:white;' >Hello " <?php echo $session; ?> " world!</p></table>
  </div>

The result is an empty string that shows: Hello " " World! Anyone know what I might be doing wrong?

Zach C
  • 21
  • 9
  • Why on earth people need to be down-voting this question. People might have different knowledge, please –  Apr 24 '13 at 06:42

5 Answers5

1

Try like this:

var session = '<?php echo $session; ?>';
karaxuna
  • 26,752
  • 13
  • 82
  • 117
  • I tried that and hopefully it will work when I eventually get the "$session" variable to recognize on the page. Right now it's still not working because my GET is wrong somewhere. Thanks for the help. – Zach C Apr 24 '13 at 06:45
1

I suggest stop using names like 'session' that might conflict with php reserved words. Maybe that's not the issue here, but avoids many other problems and confusions.

Also in javascript put the echo in quotes:

    var session = '<?php echo $session; ?>';
Aris
  • 4,643
  • 1
  • 41
  • 38
  • Yeah, I thought of that and actually changed the name but when it didn't work I changed it back. I'll fix it just for clarity. Thanks. – Zach C Apr 24 '13 at 07:18
  • also instead of trying to debug in the javascript, do this in PHP. do an echo there to make sure that the variable is ok and then see why js is not working. – Aris Apr 24 '13 at 07:21
  • Yeah, I added some additional code above to show what I'm trying to do to verify that the variable is passed. So far, no luck. – Zach C Apr 24 '13 at 07:30
  • can you show the complete URL of the second php file? – Aris Apr 24 '13 at 08:04
  • Sorry about the delay but I haven't been on in a couple of days. Here is the URL that shows when I select the Google Earth page. http://localhost/satellites/web_access/3D-Earth.php?session=19572 The 5 digit number is generated randomly to hold the session data and matches the folder that it creates when I generate the KML. – Zach C Apr 27 '13 at 01:43
  • your URL seems correct and should work with $_GET. maybe you have some redirects on the webserver that changes it? – Aris Apr 27 '13 at 05:33
  • Do you have any idea how I could change that? What configuration file? – Zach C Apr 27 '13 at 06:04
  • in the root of the localhost there is usually an .htaccess file. If you have one, maybe thats the problem. you can try to remove it and see what happens. – Aris Apr 27 '13 at 06:51
  • No luck. I found a number of .htaccess files in the phpmyadmin folder as well as the apache folder but removing them didn't seem to do anything. – Zach C Apr 29 '13 at 03:10
  • well, put them back in now :) so I don't know what's wrong. if you can put a live version on the web, I may be able to assist. – Aris Apr 29 '13 at 08:48
0

You should put a single quotes around your variable like this

var session = '<?php echo $session; ?>';
Rikesh
  • 26,156
  • 14
  • 79
  • 87
chandresh_cool
  • 11,753
  • 3
  • 30
  • 45
0

As above answers say, you have to have single quotes around a php statement when you want to use it in javascript. So var session = 'php statement'; is the right way to go.

tornados
  • 86
  • 5
0

have you tried to put this code

  <script>
  var session = <?php echo $session; ?>;
  </script>

under this code ??

 <?php
  $session = $_GET["session"];
 ?>