0

Is it possible in the line that begins with $sql = to use the variables that were defined earlier in javascript?

var southWestLat = map.getBounds().getSouthWest().lat();
var southWestLng = map.getBounds().getSouthWest().lng();
var northEastLat = map.getBounds().getNorthEast().lat();
var northEastLng = map.getBounds().getNorthEast().lng();

var coordinatesMap =
<?php
    global $wpdb;
    $sql = "SELECT user_id, lat, lng FROM coordinates WHERE lat>southWestLat and lat<northEastLat and lng>southWestLng and lng<northEastLng";
    $rows = $wpdb->get_results($sql, OBJECT_K);
    ...
?>;
Ash
  • 1,289
  • 3
  • 17
  • 24

3 Answers3

2

Since javascript is a client side language if is not possible to directly use them. You can however use AJAX to transfer values of those JS variables to the server. Execute your sql statement and return back the results in some JSON object for example.

HTH :)

Oki so say we have the following in JS

var myvar1 = "HELLO WORLD 1";
var myvar2 = "HELLO WORLD 2";

Now we want to send myvar to the server via AJAX. Here is how.

$.ajax({
   type: "POST",
   url: "some.php",
   data: ({'action' : 'action1' , 'myvar1' : myvar1 , 'myvar2' : myvar2 }),
   success: function(msg){
     alert( "Data Saved: " + msg );
   }
 });

The url must be some script that will handle the ajax request. What will be echoed by that script will end up in the msg variable of the success callback.

AJAX Handler

To do this you will need to send an extra parameter with each AJAX request. If you look at the above example I added the action parameter. This will allow us to identify which action should be executed by the AJAX Hander.

ajaxhandler.php

switch($_POST["action"]) {
    case "action1":
        action1($_POST["myvar1"]);
    break;
    case "action2":
        action2($_POST["myvar2"]);
    break;
}

function action1($myvar1){
    do your action logic here
}

function action2($myvar2){
    do your action logic here
}
Gabriel Spiteri
  • 4,896
  • 12
  • 43
  • 58
  • @ElGabbu , thanks. A few questions - 1. what's the syntax for sending more than one parameter? 2. how do I address those parameters on the php file? 3. Is it possible in one php file to deal with more than one ajax request? how? – Ash May 30 '11 at 09:25
  • @Ash 1. I updated the question to show you how to send multiple parameters. 2. To retrieve those parameter in the PHP file you use the $_POST super global variable like so $_POST["myvar1"]. We are using POST because we specified so in the type option in JS. We could have used "GET" and would have the data in $_GET["myvar1"]. 3. I know what you mean you can have a sort AJAX Handler file. I will update the answer to give you an idea how you can do it :) – Gabriel Spiteri May 30 '11 at 09:43
  • @ElGabbu , thanks a lot. It's all making sense. But it seems something very basic isn't working for me. I'm trying to simplify everything just to see it works. This is my php file: ``. And I'm using your first script, the one with one variable passed. But it doesn't work. Is writing `"some.php"` in the url field enough? no path needed before? – Ash May 30 '11 at 10:10
  • it depends on the location of some.php. If some.php is in the root of the website you should be able to use /some.php otherwise I sugest the full path from the root like /mypath/some.php – Gabriel Spiteri May 30 '11 at 11:12
  • @ElGabbu , the problem is with a missing `,` after the data part. If you can edit it in it would probably help someone in the future. Thanks for your help. – Ash May 30 '11 at 11:30
  • @Ash my apologies for the mistake. I edited the answer with the correction. – Gabriel Spiteri May 30 '11 at 11:39
0

PHP runs on the server while JavaScript runs in the client's browser.

So you need to use AJAX to do this - have a look at jQuery's AJAX functions.

ThiefMaster
  • 310,957
  • 84
  • 592
  • 636
0

no, it's absolutely different things. javascript executes on client side, php code executes on server side. your objective is possible only if you pass your js values to php script via HTTP request

heximal
  • 10,327
  • 5
  • 46
  • 69