1

ok so im trying to get a javascript variable into a php script, this would be an example,

<script type="text/javascript">
x = new Date()
</script>
<?php
$x = $_GET["x"]; 
?>
<p><?php echo $x ?></p>

i just cant get it to work! I need help please?

EDIT: well im just trying to get the hash from a url via javascript then put that in a php script.

7
  • Those two different languages execute in completely different places. The javascript executes on the client, and the php executes on the server. The only way to make one "see" the other is to use a communications framework like AJAX. Commented Apr 5, 2011 at 2:54
  • 1
    For the example, why not use php's date function? Commented Apr 5, 2011 at 2:57
  • well sorry Shad, :) im a little new to these areas of code, look at my profile im only 14 Commented Apr 5, 2011 at 2:58
  • retracted, that was harsher than you deserved. Apologies ;) Commented Apr 5, 2011 at 3:00
  • lol its fine im just learning :) Commented Apr 5, 2011 at 3:04

5 Answers 5

16

PHP and javascript don't work like that.

PHP is a server-side language. While javascript is a clientside language.

There are still ways of passing data from the client-window to your server, via ajax, query parameters, cookies... But none will work in the same page.

Give us a clearer image on what you are trying to achieve and we will gladly help.

UPDATE

JS

<script type="text/javascript">  
    document.cookie = 'name=Khez' ;  
</script>  

PHP

<?php  
    var_dump($_COOKIE['name']);  
?>  
Sign up to request clarification or add additional context in comments.

8 Comments

Updated, post if you have issues.
@Khez, how exactly will i put the data into the cookie from js
depends on the type of data you need... you use the construct I used above it's a simple key-value pair. For your example you'd do document.cookie= 'date='+(new Date()).getTime(); and in PHP you'd do $x=date($_COOKIE['date']);
im trying to get the hash and put it into a php
I don't know what hash you're referring to, but I believe it is outside the scope of your question. Readup on the PHP manual and JS on w3schools
|
6

So there are 2 pages page1.php and page2.php

page2.php needs to pass JS variables to page1.php

We can do this by passing it as url variables from page2.php and get it in page1.php using $_GET[].

page2.php (send JS variable)

<script type=text/javascript>
  var lati = location.lat();
  var longi = location.lng();
  document.location = 'http://www.rajak.me/index.php?addlat='+lati+'&addlong='+longi;   
});
</script>

page1.php (receive JS variable)

<?php    
  $addlat = $_GET['addlat'];
  $addlong = $_GET['addlong'];
?>

1 Comment

This one seems to be working fine for passing js variables of one php page to another page. This is what i was searching earlier. Thanks @Raja Krishnan for suggesting this idea.
1

You could use the following Javascript which will link to somepage.php with the variable in the Url

<script type="text/javascript">
x = new Date()
window.location.href = "somepage.php?w1=" + x;
</script>

This is the contents of somepage.php which recieves the variable and echoes it

<?php
   if (isset($_GET["w1"])) {
     $x = $_GET["w1"];
     echo $x;
   }else{
   echo 'no variable received';
   }
    ?>

Comments

0

PHP is "Server Side" code and javascript is client side code. They don't interact...

Comments

0

PHP is server-side~ all parsing is done on the server. JavaScript is client-side~ everything happens AFTER it get's to the client. If you need date in PHP, I recommend time() and or date()

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.