Skip to content Skip to sidebar Skip to footer

Passing Data From Javascript To Php Using Jquery

Maybe this question has been asked before but I am struggling in doing this. I have got a php file which does not include any piece of php code (might be in the future),it include

Solution 1:

Take a look at the accepted answer to "Javascript Post Request like a Form Submit".

It provides javascript for for:

function post_to_url(path, params, method) {
...
}

I think this will do what you want.

Solution 2:

Thanks for all the answers. I have solved the problem. The data had being passes but I was not able to handle it properly. I just add a dummy code to test it.It worked. I will upload the code after I have finished.Thanks to all.

Solution 3:

This function is in a PHP file, but is full of JS code. The last line passes the data to another PHP file which saves the data into a database.

functionsaveProfile (){

      var _profileId    = 0;
      var _profileName  = document.getElementById('nameProfile').value;

        var queryArr=[];
       $(markersArray).each(function (index){
            //alert(markersArray[index].name);var _locationId = index;
            var _locName    = markersArray[index].name;
            var _markerLat  = markersArray[index].marker.getLatLng().lat();
            var _markerLng  = markersArray[index].marker.getLatLng().lng();

            var locations = {  
                                profileName: _profileName,
                                locationId:_locationId,
                                locationName:_locName,
                                lat:_markerLat,
                                lng:_markerLng  }

            queryStr = { "locations": locations} 

            queryArr.push(queryStr);


        });

        /*for ( var i=0; i<markersArray.length; i++){
            alert(queryArr[i].locations.locationId+"--"+queryArr[i].locations.locationName +"--"+queryArr[i].locations.lat);
        }*/

          $.post('dbConn.php', { opType:"saveAsProfile" , data: queryArr}, showResult, "text");

    }

This is dbConn.php, which is called by the saveProfile method. The data is handled as follows:

$db_host = 'localhost';
    $db_user = 'root';
    $db_pass = '';
    $db_name = 'google_map_db';



    $opType = $_POST['opType'];

    //SAVE PROFILES WITH A PROFILE NAMEif(!strcmp($opType, "saveAsProfile") ){

        $res = $_POST['data'];
        $connect = mysql_connect( $db_host, $db_user, $db_pass ) ordie( mysql_error() );
        mysql_select_db( $db_name ) ordie( mysql_error() );
        $queryString = "";

        for($i = 0; $i < sizeof($res); $i++){

            $profileName  = $res[$i]['locations']['profileName'];
            $locationId   = $res[$i]['locations']['locationId'];
            $locationName = $res[$i]['locations']['locationName'];
            $lat          = $res[$i]['locations']['lat'];
            $lng          = $res[$i]['locations']['lng'];

            $sp = " ";
            $queryString =  $queryString . "(0 ".",'".$profileName."',".$locationId.",'".$locationName."',".$lat.",".$lng.") ";

            if($i<sizeof($res)-1)
                $queryString =  $queryString . ", ";

        }


        $qInsertUser = mysql_query(" INSERT INTO `map_locations` (`profileId`, `profileName`, `locationId`, `locationName`, `lat`, `lng`)
                                     VALUES  ".$queryString." ");

        if ($qInsertUser){
            echo"successfully added!!!";
        } else {
            echo"Error";
        }

    }

Post a Comment for "Passing Data From Javascript To Php Using Jquery"