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.
-
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– Chris Eberle2011年04月05日 02:54:56 +00:00Commented Apr 5, 2011 at 2:54
-
1For the example, why not use php's date function?DouglasMarken– DouglasMarken2011年04月05日 02:57:17 +00:00Commented Apr 5, 2011 at 2:57
-
well sorry Shad, :) im a little new to these areas of code, look at my profile im only 14Trevor Rudolph– Trevor Rudolph2011年04月05日 02:58:44 +00:00Commented Apr 5, 2011 at 2:58
-
retracted, that was harsher than you deserved. Apologies ;)Shad– Shad2011年04月05日 03:00:56 +00:00Commented Apr 5, 2011 at 3:00
-
lol its fine im just learning :)Trevor Rudolph– Trevor Rudolph2011年04月05日 03:04:47 +00:00Commented Apr 5, 2011 at 3:04
5 Answers 5
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']);
?>
8 Comments
document.cookie= 'date='+(new Date()).getTime(); and in PHP you'd do $x=date($_COOKIE['date']);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
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
PHP is "Server Side" code and javascript is client side code. They don't interact...
Comments
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()