views:

55

answers:

3

Hi everyone.

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 includes just javascript and some html. What I want to do is clicking a button in this php file to send some amount of data to another php file. put it this way..

1-I have got a saveProfile function in a.php and a button is calling this function

  function  saveProfile (){  
      var variableD = 'sample data';
      $.post("dbConn.php", { js: variableD});
  }

2-I have got another php which is called dbConn.php that receives data and stores in a database table.

I have found so many examples. I have applied them but it still does not work and is driving me nuts. I am a java programmer but new in php. Any help is appreciated.give me some clean sample code or if you see any mistake please kindly warn me. Thanks to all in advance...

Regards. Ozlem.

+1  A: 

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.

David Jones
jQuery uses Ajax to do the same thing, but more succinctly and without loading a new page.
outis
Hey outis, can you post an example of how to do that using jQuery/Ajax? Nice to know it is possible, but without example code, it's not much help to people reading this.
David Jones
A: 

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.

Ozlem
outis
A: 

//HERE ANSWER COMES... //THIS FUNCTION IS IN A PHP FILE BUT FULL OF JS CODE //THE LAST LINE PASSES THE DATA TO ANOTHER PHP FILE WHICH SAVES THE DATA INTO A DB

    function  saveProfile (){

      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 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 NAME
    if(!strcmp($opType, "saveAsProfile") ){

        $res = $_POST['data'];
        $connect = mysql_connect( $db_host, $db_user, $db_pass ) or die( mysql_error() );
        mysql_select_db( $db_name ) or die( 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";
        }

    }

//I HOPE THIS HELPS TO OTHER PEOPLE... //CHEERS.. Ozlem.

Ozlem