Results 1 to 2 of 2

Thread: Get data from Javascript to PHP - Possible?

  1. #1
    Join Date
    Apr 2009
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Get data from Javascript to PHP - Possible?

    I'm planning a project and I'm a complete beginner so will be learning new skills to achieve this. I haven't started yet because I don't yet know which tools to use for the job. I think it comes down to a question of sharing data between Javascript and PHP.

    What I want my web page to do (and the tools I think I should use) -

    (1) Geocode a UK postcode using Google Maps API and display this on the map (Javascript/AJAX unless there's a PHP way).

    (2) Extract the latitude and longitude of the chosen point on the map (javascript?)

    (3) Convert to UK grid (could be either javascript or php, I already have a javascript example, but it's translatable. )

    (4) Use that UK grid reference to query a database of historic wind values which has >1M records and is therefore, I think, way to big to be handled client side. (?) I have the database as a csv. I think the size of this database is the primary reason I can't do the whole thing in javascript and I don't think javascript is capable of querying a remote database (?). (php,mySQL)

    (5) Use the wind value from the database to estimate the likely performance of a wind turbine at that location. This is a complicated calculation which involves some quasi-confidential business data, so it's preferable but not essential that it reside server-side. This is the secondary reason I'd rather not do it all in javascript. (php)

    (6) Display predictions of wind turbine performance and income for the geocoded location. (php)

    (7) Update those results if the marker on the google map is moved to a new location. (javascript to php again).


    Now, by googling, I've found conflicting opinions as to whether it's possible to pass information from javascript to be dealt with by php. This is a potential stumbling block. I understand that the php code runs on the server and sends html or javascript output to the browser to render, so once that's done it's too late to use information obtained using javascript to the php logic. But I'm imagining there must be a way to force a refresh of the php using the latitude and longitude obtained from javascript?

    Can I accomplish this with a cookie? Both javascript and php can read and write cookies, so can I bridge the divide this way?

    Or should I be researching GET and POST methods for creating a new page?

    Or should I be generating the javascript for the google map dynamically using php?

    I'm a bit lost and would really appreciate some pointers before I start out along a path where the bridge is out. I'm a beginner, so I will not feel patronised by very simplistic "101" type explanations.

    Is my project possible?

    Many thanks.

    Frasmar

  2. #2
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Without getting into a complete analysis of it, the basic answer is that Javascript cannot "query" a remote database, but it may use AJAX to do so indirectly. AJAX requests the results of a PHP script, so basically you can setup a php page that will take the input (let's say var=1), then return not html but just the information you want (let's say the result of '1'-> 'london', or whatever).
    If you setup the PHP as easily queriable, note two things:
    1. This will basically simulate Javascript interacting with the DB and should be relatively fast as long as you don't need to do many repeated queries-- try to limit the AJAX and get it all done with one PHP call for each goal.
    2. Be sure to take care of security-- by making an easy-to-input system for your database, you need to make sure that you don't let injection be a risk, so escape all input and don't do any dynamic queries (hard code the MySQL queries in the php, NOT directly from the Javascript).

    This sounds, generally, no more complex than Google maps in the first place, so sure it's possible, but it may be a lot of work. The hardest part is working out the system and making it smooth, but in a technical sense this isn't really that hard (aside from interacting with Google maps, maybe).

    Just remember-- PHP is a serverside language that can do things serverside only and will just output plain text for you (which can be html). Javascript is an interactive scripting language that is performed on the client side-- it can't access the server. Using AJAX will allow them to work together, but Javascript cannot access the server (just request a "page") and PHP cannot be directly interacted with (beyond loading a page, sending a request, etc.).
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •