Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Here I have a script which help me to get places from google places API. So now I want to store all this into mysql but how? I'm new to mysql and php, and how to store data that I get from google places to database?

What I need to do here? Can someone show me on my example...

How to combine php and javascript;

CODE: http://jsbin.com/AlEVaCa/1

So I need to store data which I got from google:

google.maps.event.addListener(marker,'click',function(){
        service.getDetails(request, function(place, status) {
          if (status == google.maps.places.PlacesServiceStatus.OK) {
            var contentStr = '<h5>'+place.name+'</h5><p>'+place.formatted_address;
            if (!!place.formatted_phone_number) contentStr += '<br>'+place.formatted_phone_number;
            if (!!place.website) contentStr += '<br><a target="_blank" href="'+place.website+'">'+place.website+'</a>';
            contentStr += '<br>'+place.types+'</p>';
            infowindow.setContent(contentStr);
            infowindow.open(map,marker);
          } else { 
            var contentStr = "<h5>No Result, status="+status+"</h5>";
            infowindow.setContent(contentStr);
            infowindow.open(map,marker);
          }
        });

    });

I want to store all place.name,website ... etc. data to mydatabase. How to do that? Is there any way to store this data?

share|improve this question

2 Answers 2

up vote 2 down vote accepted

Use AJAX to send data to PHP file.

Use jQuery $.post()-AJAX method to send data to php file

 data = "name="+name+"&place="+website;
 $.post('file_to_store.php', data, function(data) {
     //Here you can get the output from PHP file which is (data) here
 });

Pure javascript way

function loadXMLDoc()
{
   var xmlhttp;
   if (window.XMLHttpRequest){
      // code for IE7+, Firefox, Chrome, Opera, Safari
      xmlhttp=new XMLHttpRequest();
   }
   else{
      // code for IE6, IE5
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
   }

   xmlhttp.onreadystatechange=function(){
     if (xmlhttp.readyState==4 && xmlhttp.status==200)
     {
        document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
     }
   }

   data = "name="+name+"&place="+website;
   xmlhttp.open("POST","file_to_store.php",true);
   xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
   xmlhttp.send(data);
}

In file_to_store.php receive all data from $_POST[] global array

 if(isset($_POST)){
   $name = $_POST['name'];
   $website = $_POST['website'];
   //Do same for all other variables

   //Steps to insert Data into Database
   //1. Connect to database 
   //2. Select Database
   //3. Generate Database Insert Query
   //4. Run mysql Query to insert

   // Return appropriate return back to Javascript code - Success or Failure 
 }
share|improve this answer
    
yes bt how to make a loop here to put data to mysql: jsbin.com/AlEVaCa/3/edit –  Marco Jordan Sep 11 '13 at 11:50
    
$con=mysqli_connect(HOST, USER, PASSWORD, DATABASE); // Check connection if (mysqli_connect_errno()) { echo "Failed to connect to MySQL: " . mysqli_connect_error(); } mysqli_query($con,"INSERT INTO tablename (Place, Name, Website) VALUES ('USA', 'Marco Jordan',35)"); mysqli_close($con); –  Kailash Ahirwar Sep 11 '13 at 12:05

use serialize($data) then put it to database, use unserialize() after getting data from db.

addition: this will store raw data, you'll probably need a parser also.

addition2: sorry i assumed that you got an array,

alternative solution if you got non array data: you can use base64_encode($raw_data) to store and base64_decode($encoded_data) to use the encoded data coming from sql.

share|improve this answer
    
but how this will looks like in my example ... –  Marco Jordan Sep 11 '13 at 11:14
    
i assumed that you've already got raw data from google. are you asking retrieving data from google? –  alpera Sep 11 '13 at 11:16
    
Yes I got raw data and how to store it now? json_encode ??? –  Marco Jordan Sep 11 '13 at 11:17
    
can you post a raw data example please? –  alpera Sep 11 '13 at 11:21
    
here i get data: jsbin.com/AlEVaCa/1 –  Marco Jordan Sep 11 '13 at 11:29

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.