View previous topic :: View next topic |
Author |
Message |
LiquidCode Moderator Team
Joined: 05 Dec 2000 Posts: 1751 Location: Space and Time
|
Posted: Tue Jan 10, 2012 2:30 pm Post subject: Javascript/PHP help |
|
|
I want to pass a PHP var to Javascript. I am trying this but it is not working. Can anyone help? Thanks!
Code: |
window.location='http://insuresign.com/gloc/writeloc.php?loc=' + lat + '|' + long + '&id='<?php echo $id; ?>;
|
_________________ Chris
Http://theblindhouse.com |
|
Back to top |
|
|
PGWARE Web Host
Joined: 29 Dec 2001 Posts: 1563
|
Posted: Wed Jan 11, 2012 5:57 pm Post subject: |
|
|
Code: |
<?php
$id = 'testing';
echo "window.location='http://insuresign.com/gloc/writeloc.php?loc=' + lat + '|' + long + '&id='$id;";
?>
|
Try that |
|
Back to top |
|
|
LiquidCode Moderator Team
Joined: 05 Dec 2000 Posts: 1751 Location: Space and Time
|
Posted: Wed Jan 11, 2012 6:31 pm Post subject: |
|
|
Thanks for the reply, but no. Didn't work. Here is the full code if you have a min and can take a look.
I am passing the ID to this script then want to write that info + the location info to a file using another PHP script. If I just have it alert the location info it works fine, but not when trying to write to a file with the passed ID.
Code: |
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<?php
$id = $_GET['id'];
// echo $id;
?>
<script type="text/javascript" charset="utf-8">
function findLocation() {
navigator.geolocation.getCurrentPosition(foundLocation, noLocation);
}
function foundLocation(position)
{
var lat = position.coords.latitude;
var long = position.coords.longitude;
// alert('Found location: ' + lat + ', ' + long);
<?php
// $id = 'testing';
echo "window.location='http://insuresign.com/gloc/writeloc.php?loc=' + lat + '|' + long + '&id='$id;";
?>
// window.location='http://insuresign.com/gloc/writeloc.php?loc=' + lat + '|' + long + '&id='<?php echo $id; ?>;
// var url = 'http://insuresign.com/gloc/writeloc.php?loc=' + lat + '|' + long;
// alert(url);
// window.location=url;
}
function noLocation()
{
alert('Could not find location');
}
</script>
<body onload="findLocation()">
<!-- <div id="map_canvas" style="width: 100%; height: 100%"></div> -->
</body>
</html>
|
_________________ Chris
Http://theblindhouse.com |
|
Back to top |
|
|
PGWARE Web Host
Joined: 29 Dec 2001 Posts: 1563
|
Posted: Wed Jan 11, 2012 7:22 pm Post subject: |
|
|
It was a problem with the ' not being at the end of the variable, any strings that arent variables have to be encased in ' single quotes or the javascript just errors out, javascript is real picky on syntax. I just tested this and it forwards to the page. Very nice script btw, I was needing to get location data for a project I was working on.
Code: |
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<?php
$id = $_GET['id'];
?>
<script type="text/javascript" charset="utf-8">
function findLocation() {
navigator.geolocation.getCurrentPosition(foundLocation, noLocation);
}
function foundLocation(position)
{
var lat = position.coords.latitude;
var long = position.coords.longitude;
// alert('Found location: ' + lat + ', ' + long);
<?php
echo "window.location='http://'http://insuresign.com/gloc/writeloc.php?loc=' + lat + '|' + long + '&id=$id';";
?>
}
function noLocation()
{
alert('Could not find location');
}
</script>
<body onload="findLocation()">
<!-- <div id="map_canvas" style="width: 100%; height: 100%"></div> -->
</body>
</html>
|
You might also want to use $id = urlencode($id) incase spaces or other characters are used before passing it to the window.location javascript func. |
|
Back to top |
|
|
LiquidCode Moderator Team
Joined: 05 Dec 2000 Posts: 1751 Location: Space and Time
|
Posted: Wed Jan 11, 2012 7:54 pm Post subject: |
|
|
Cool, thanks. I figured out a way to get it to work also.
Code: |
var phpid = "<?php echo $id; ?>";
|
Should have thought of this first. Thanks for the help! The ID is a MD5 of a string so there should not be any spaces, but I think I may add that just in case anyway. _________________ Chris
Http://theblindhouse.com |
|
Back to top |
|
|
|