2

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?

3 Answers 3

2

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 
 }
Sign up to request clarification or add additional context in comments.

2 Comments

yes bt how to make a loop here to put data to mysql: jsbin.com/AlEVaCa/3/edit
$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);
0

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

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

Addition 2: 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.

4 Comments

i assumed that you've already got raw data from google. are you asking retrieving data from google?
can you post a raw data example please?
i only can see a map here, give me text data
click on submit and you will get a data
0

Fundamentally, your JavaScript program, executing on the client side, does not have direct access to the SQL database on the host. You must use AJAX to issue requests to the host, and the host-side software must be programmed to handle them. Lots(!) of existing tutorials on this subject are already out there ... everywhere.

Comments

Your Answer

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.