1

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.

casablanca
70.9k8 gold badges139 silver badges155 bronze badges
asked Oct 25, 2010 at 0:24
2
  • 1
    Can you post the code in dbConn.php? Commented Oct 25, 2010 at 0:28
  • 1
    It would also help if you clarified what "does not work" means. It crashes? You aren't getting an entry in the database? Your jQuery dies? Please clarify. Commented Oct 25, 2010 at 1:05

3 Answers 3

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.

answered Oct 25, 2010 at 2:03
Sign up to request clarification or add additional context in comments.

2 Comments

jQuery uses Ajax to do the same thing, but more succinctly and without loading a new page.
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.
0

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.

answered Oct 25, 2010 at 3:34

1 Comment

You should add an example of the problem code to the question, then post your solution in your answer so that others can learn. Doing so will also better match SO's Q&A format. As it is, this "answer" is more like a discussion.
0

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.

 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 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 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";
 }
 }
zondo
20.4k8 gold badges50 silver badges89 bronze badges
answered Oct 25, 2010 at 23:58

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.