Jump to content

Run Javascript From PHP


hobbiton73

Recommended Posts

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

 

I'm currently running the code below (originating from nearby.org.uk) to convert OS Grid References to Latitude and Longtiude co-ordinates.

 

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";
    }

}

 

This code is run through a 'click' event from a button on a HTML form and then the record is saved to a mySQL database.

 

However, once a month, I have a list of approx 20,000 OS Grid References which need converting and saving to the same mySQL database. To do this manually on a button click wouldn't be very practical, so I've been working on doing this automatically.

 

I may very well be doing this the wrong way, but I've been using the PHP script below to extract the two pieces of information I need from the xml file, the first is the 'Name, and the second is the 'NGR', and it is this field that needs converting.

 

<? 

  $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>";  
  } 


?>

 

This is where I've become a little stuck because I'm not sure how to get the data converted and to a point where it can be saved to the relevant fields in my database.

 

I just wondered whether someone would perhaps take a look at this please and let me know what I need to do.

 

Many thanks

 

 

Link to comment
Share on other sites

You can't "run" JavaScript via PHP. PHP is run on the server and JavaScript is run on the client. I would think you should be looking for a PHP solution to generate the results. But, if you cannot find one, then one option would be to use PHP to generate a page with ALL the records you need to process, then create a JavaScript function to process each record on the page. But, instead of trying to update each record in the database as it is processed, you could instead have the results stored in form fields on the page. Then once all the records are processed, submit the page one time and have a PHP page to update all the records simultaneously.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.