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.

Trevor Rudolph
  • 1,055
  • 4
  • 18
  • 42
  • 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. – Chris Eberle Apr 05 '11 at 02:54
  • 1
    For the example, why not use php's date function? – DouglasMarken Apr 05 '11 at 02:57
  • well sorry Shad, :) im a little new to these areas of code, look at my profile im only 14 – Trevor Rudolph Apr 05 '11 at 02:58
  • retracted, that was harsher than you deserved. Apologies ;) – Shad Apr 05 '11 at 03:00
  • lol its fine im just learning :) – Trevor Rudolph Apr 05 '11 at 03:04
  • This doesn't look like a duplicate to me. This question is about getting variables *from JavaScript to PHP*. The duplicate is about getting variables *from PHP to JavaScript*, which is entirely different. Voting to re-open. – showdev Feb 04 '15 at 23:15
  • Perhaps a more appropriate duplicate would be: [What is the difference between client-side and server-side programming?](http://stackoverflow.com/questions/13840429/what-is-the-difference-between-client-side-and-server-side-programming) – showdev Feb 04 '15 at 23:40

5 Answers5

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']);  
?>  
Khez
  • 10,172
  • 2
  • 31
  • 51
  • Updated, post if you have issues. – Khez Apr 05 '11 at 02:58
  • @Khez, how exactly will i put the data into the cookie from js – Trevor Rudolph Apr 05 '11 at 03:01
  • 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']);` – Khez Apr 05 '11 at 03:06
  • im trying to get the hash and put it into a php – Trevor Rudolph Apr 05 '11 at 03:07
  • 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](http://www.php.net/manual/en/index.php) and [JS on w3schools](http://www.w3schools.com/js/default.asp) – Khez Apr 05 '11 at 03:12
  • @deceze: `W3Schools should consider wikifying their content so the community could self-correct and keep the information up-to-date. Today, they do not even allow you to submit corrections on a page. They should.` I want that so much, you have no idea. – Khez Apr 05 '11 at 03:24
  • @Khez In the meantime, they may have decent introductions to the basics of a topic, but whenever linking to them we should include the link to w3fools as a caveat emptor. :) – deceze Apr 05 '11 at 03:31
  • @deceze: Would you be surprised if I say I will? ^^ – Khez Apr 05 '11 at 03:33
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'];
?>
nhahtdh
  • 55,989
  • 15
  • 126
  • 162
Raja Krishnan
  • 321
  • 1
  • 3
  • 11
  • 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. – Sadesh Kumar N Jul 12 '12 at 03:21
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';
   }
    ?>
Dcdanny
  • 469
  • 2
  • 8
0

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

John Sobolewski
  • 4,512
  • 1
  • 20
  • 26
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()

Shad
  • 15,134
  • 2
  • 22
  • 34