Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > PHP How-To
|
PHP How-To Post your "How do I do this with PHP?" questions here.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the PHP How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 8th, 2011, 11:40 AM
Registered User
 
Join Date: Oct 2011
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Pass PHP Variable to Javscript Function

I wonder whether someone may be able to help me please.

I am using the code below to extract a 'Name' and 'National Grid Reference' from an xml file.

Code:
<? 

  $objDOM = new DOMDocument(); 
  $objDOM->load("scheduledmonuments.xml"); 

  $Details = $objDOM->getElementsByTagName("Details"); 
  foreach( $Details as $value ) 
  { 
    
    $Name = $value->getElementsByTagName("Name"); 
    $name  = $Name->item(0)->nodeValue; 

    $NGR = $value->getElementsByTagName("NGR"); 
    $ngr  = $NGR->item(0)->nodeValue; 

    echo "$name :: $ngr <br>";  
  } 
?>
What I would like to be able to do is to is to carry forward the 'Name' but also convert the 'NGR' to a Latitude and Longitude co-ordinate.

I have the conversion code written as a Javascript function (originally posted on nearby.org.uk), see below.

Code:
function converttolatlng() {
    var gr = document.getElementById('osgridref').value;

    var osgb = new GT_OSGB();


    if (osgb.parseGridRef(gr)) {
        var wgs84 = osgb.getWGS84();

        document.getElementById('osgb36lat').value = wgs84.latitude;
        document.getElementById('osgb36lon').value = wgs84.longitude;
    }
    else {
        document.getElementById('osgb36lat').value = "n/a";
        document.getElementById('osgb36lon').value = "n/a";
    }

}
At the moment the code takes and places the information into text boxes on a form.

But what I would like to do is to carry forward the 'Name' and convert the 'NGR' values from the PHP file rather than text boxes, posting the results on screen, but I'm not sure how, or whether it can be done.

Could someone perhaps take a look at this please and let me know what I need to do to get this to work.

Many thanks
 
Old October 9th, 2011, 05:12 PM
Friend of Wrox
 
Join Date: May 2011
Posts: 125
Thanks: 0
Thanked 24 Times in 24 Posts
Default

Greetings,

Yes it's possible as long as it's php that sends the html/javascript to the browser.

Example:
PHP Code:
<?php

$objDOM 
= new DOMDocument(); 
$objDOM->load("scheduledmonuments.xml"); 

$Details $objDOM->getElementsByTagName("Details"); 
foreach( 
$Details as $value 

    
$Name $value->getElementsByTagName("Name"); 
    
$name  $Name->item(0)->nodeValue

    
$NGR $value->getElementsByTagName("NGR"); 
    
$ngr  $NGR->item(0)->nodeValue

//    echo "$name :: $ngr <br>";  


?>
<html>
<head>
<title>title</title>
<script language="javascript" type="text/javascript">
function converttolatlng() {
//    var gr = document.getElementById('osgridref').value;
    var gr = <?php echo $ngr?>;
    var osgb = new GT_OSGB();


    if (osgb.parseGridRef(gr)) {
        var wgs84 = osgb.getWGS84();

        document.getElementById('osgb36lat').value = wgs84.latitude;
        document.getElementById('osgb36lon').value = wgs84.longitude;
    }
    else {
        document.getElementById('osgb36lat').value = "n/a";
        document.getElementById('osgb36lon').value = "n/a";
    }

}
</script>
</head>

<body>

</body>
</html>





Similar Threads
Thread Thread Starter Forum Replies Last Post
Pass PHP variable to JavaScript function jpindi Javascript How-To 2 September 9th, 2009 11:41 AM
VBscript boolean function causes Javscript to bomb calgarychinese Javascript 2 August 25th, 2008 03:38 PM
How can I pass a variable from php to Javascrip aneesh_sunu@yahoo.co.in PHP How-To 2 May 17th, 2007 06:50 AM
pass java variable to xsl variable kathy1016cats XSLT 1 June 14th, 2006 06:23 PM
pass variable into function and retaining value mikeuk Beginning PHP 3 August 4th, 2004 06:47 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.