forum.vdsworld.com Forum Index forum.vdsworld.com
Visit VDSWORLD.com
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


Javascript/PHP help

 
Post new topic   Reply to topic    forum.vdsworld.com Forum Index -> Miscellaneous
View previous topic :: View next topic  
Author Message
LiquidCode
Moderator Team


Joined: 05 Dec 2000
Posts: 1711
Location: Space and Time

PostPosted: Tue Jan 10, 2012 2:30 pm    Post subject: Javascript/PHP help Reply with quote

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://vdsfreak.com
Back to top
View user's profile Send private message Send e-mail Visit poster's website
PGWARE
Web Host


Joined: 29 Dec 2001
Posts: 1537

PostPosted: Wed Jan 11, 2012 5:57 pm    Post subject: Reply with quote

Code:

<?php
  $id = 'testing';

  echo "window.location='http://insuresign.com/gloc/writeloc.php?loc=' + lat + '|' + long + '&id='$id;";

?>


Try that
Back to top
View user's profile Send private message
LiquidCode
Moderator Team


Joined: 05 Dec 2000
Posts: 1711
Location: Space and Time

PostPosted: Wed Jan 11, 2012 6:31 pm    Post subject: Reply with quote

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://vdsfreak.com
Back to top
View user's profile Send private message Send e-mail Visit poster's website
PGWARE
Web Host


Joined: 29 Dec 2001
Posts: 1537

PostPosted: Wed Jan 11, 2012 7:22 pm    Post subject: Reply with quote

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
View user's profile Send private message
LiquidCode
Moderator Team


Joined: 05 Dec 2000
Posts: 1711
Location: Space and Time

PostPosted: Wed Jan 11, 2012 7:54 pm    Post subject: Reply with quote

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! Smile 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://vdsfreak.com
Back to top
View user's profile Send private message Send e-mail Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    forum.vdsworld.com Forum Index -> Miscellaneous All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You can attach files in this forum
You can download files in this forum




Twitter@vdsworld       RSS

Powered by phpBB © 2001, 2005 phpBB Group