简体   繁体   中英

Communication between Javascript and MYSQL

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.

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM