0

I am working on some code using Google Maps API. To sum up shortly, I have MySQL database with a table of information used to generate markers on the map. I connected to the database and am using PHP to draw out the necessary attributes and communicate with my Javascript code using XML.

What I'm currently attempting to do is go in the other direction, I'm trying to send a string of information (for example "1,2,3,45,18") from my Javascript code to MySQL to be set as a session parameter (call it @sparam). What is the process behind passing this value to MySQL?

Would I be able to access a MySQL variable through PHP in the same way I can access tables (for the purpose of getting a value back into Javascript)?

I'd appreciate any insight.

Thanks.

EDIT

Maybe I was unclear in my original post. What I'm asking is how would I be able to pass a string to a MySQL session variable, specifically a set of IDs directly related to the IDs in the table of the MySQL database, and then be able to work with these IDs by calling the necessary procedures in MySQL. In turn, the procedures called in MySQL would generate some output, which would then have to be passed back to the Javascript code.

1
  • This question is not about the Google Maps API V3. (tag removed) Commented Nov 26, 2012 at 6:22

1 Answer 1

2

I created a special JSON (JavaScript Object Notation) php pages that I would call from javascript. Then I would parse those JSON responses.

Simple example:

JAVASCRIPT:

function getCheckedUnits() {
    jQuery(function($) {    
        $.ajax( {           
            url : "page_json.php?action=getsession",
            type : "GET",
            success : function(data) {
                //Get Json and loop over it's data
                if (data.length>10){
                    var jsonData = JSON.parse(data);

                    $.each(jsonData, function(Idx, Value) { 
                        if  (Idx>0){
                            //get values for each vehicle and then remove it's marker from the map and then add new marker on the map (thereofore update the marker)
                            c_latitude = Value["lat"];
                            c_longitude = Value["lon"];
                            c_name = Value["name"];
                            c_notes= Value["notes"];
                            removeMarker(c_name); //remove old marker function
                            addMarker(c_latitude, c_longitude, c_name); //add current marker function
                        }
                    });
                }
            }
        });
    });
}

PHP: Here I loop over my arrayList and then create a simple array with values. Then I just output it as a json string

    foreach ($listOfCars->arrayList as $key => $value) {

        $unit = new fleetUnit();
        $unit = $value;
        //create array for json output
        $data[] = array('lat' => $unit->lat,
            'lon' => $unit->lon, 'name' => $unit->name, 'notes' => $unit->notes);
    }

    echo json_encode($data);
Sign up to request clarification or add additional context in comments.

4 Comments

Unfortunately I'm not familiar with JSON, is there any way to do this with XML?
I added simple example how to create JSON in php. It is essentially the same as XML, but different structure. Very simple. Here is json example: maps.googleapis.com/maps/api/geocode/…
I think I may have been unclear in my original post (or maybe I'm misunderstanding the example you posted). I'm attempting to pass the string to MySQL and set a session variable with the string information.
Well, the same thing - use the javascript function that I provided and call php page and supply updated values url : "page_json.php?value1=blah1&value2=blah2&value3=blah3" In php page parse those values and process them via regular PHP. Then output your text. I showed you how to handle returned values. If you don't want to parse JSON, just process your response the way you want!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.