weixin_33705053 2018-01-27 16:37 采纳率: 0%
浏览 34

Google Map + Ajax + Mysql

I want to take variables location, location1, location2 and put them to my base in mysql. I have problem edit google script. My target is to save 3 variables, when client want to find place.
I was looking for some advices or code in internet. I am a beginner, so sorry for basic code. Link http://mikozniak.nazwa.pl/dzis/lok.php Here is my code:

google.maps.event.addDomListener(window, 'load', intilize);
function intilize() {
    var autocomplete = new google.maps.places.Autocomplete(document.getElementById("txtautocomplete"));

    google.maps.event.addListener(autocomplete, 'place_changed', function () {



    var place = autocomplete.getPlace();

    var location = place.formatted_address;
    var location1 = place.geometry.location.lat();
    var location2 = place.geometry.location.lng();

    document.getElementById('lblresult').innerHTML = location
    document.getElementById('lblresult1').innerHTML = location1
    document.getElementById('lblresult2').innerHTML = location2
    });

};



     function ajaxFunction() {
        var ajaxRequest;  // The variable that makes Ajax possible!

        try {        
           // Opera 8.0+, Firefox, Safari
           ajaxRequest = new XMLHttpRequest();
        } catch (e) {

           // Internet Explorer Browsers
           try {
              ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
           } catch (e) {

              try {
                 ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
              } catch (e) {
                 // Something went wrong
                 alert("Your browser broke!");
                 return false;
              }
           }
        }

        // Create a function that will receive data
        // sent from the server and will update
        // div section in the same page.
        ajaxRequest.onreadystatechange = function() {

           if(ajaxRequest.readyState == 4) {
              var ajaxDisplay = document.getElementById('ajaxDiv');
              ajaxDisplay.innerHTML = ajaxRequest.responseText;
           }
        }

        // Now get the value from user and pass it to
        // server script.
        var location = document.getElementById('lblresult').value;
        var location1 = document.getElementById('lblresult1').value;
        var location2 = document.getElementById('lblresult2').value;


        queryString +=  "&location = " + location + "&location1 = " + location1 + "&location2 = " + location2;
        ajaxRequest.open("GET", "ajax-example.php" + queryString, true);
        ajaxRequest.send(null); 
     }









</script>


<span>Miejsce:</span><input type="text" id="txtautocomplete" style="width:200px" placeholder="wpisz miejsce"/>

<label id="lblresult"></label>
<label id="lblresult1"></label>
<label id="lblresult2"></label>
 <div id = 'ajaxDiv'>Your result will display here</div>

 <?php
$dbhost = "mikozniak.nazwa.pl";
$dbuser ="user";
$dbpass = "password";
$dbname =  "name";

//Connect to MySQL Server
mysql_connect($dbhost, $dbuser, $dbpass);

//Select Database
mysql_select_db($dbname) or die(mysql_error());

// Retrieve data from Query String
$loc = $_GET['location'];
$loc1 = $_GET['location1'];
$loc2 = $_GET['location2'];

// Escape User Input to help prevent SQL Injection
$loc = mysql_real_escape_string($loc);
$loc1 = mysql_real_escape_string($loc1);
$loc2 = mysql_real_escape_string($loc2);

//build query
$query = "INSERT INTO `miejsca`(`idm`, `dl`, `szr`) VALUES (NULL,$loc1,$loc2)";

//if(is_numeric($age))
  // $query .= " AND age <= $age";

//if(is_numeric($wpm))
  // $query .= " AND wpm <= $wpm";

//Execute query
$qry_result = mysql_query($query) or die(mysql_error());
/*
//Build Result String
$display_string = "<table>";
$display_string .= "<tr>";
$display_string .= "<th>Name</th>";
$display_string .= "<th>Age</th>";
$display_string .= "<th>Sex</th>";
$display_string .= "<th>WPM</th>";
$display_string .= "</tr>";

// Insert a new row in the table for each person returned
while($row = mysql_fetch_array($qry_result)) {
   $display_string .= "<tr>";
   $display_string .= "<td>$row[name]</td>";
   $display_string .= "<td>$row[age]</td>";
   $display_string .= "<td>$row[sex]</td>";
   $display_string .= "<td>$row[wpm]</td>";
   $display_string .= "</tr>";
}

echo "Query: " . $query . "<br />";
$display_string .= "</table>";

echo $display_string;
*/
?>
  • 写回答

1条回答 默认 最新

  • weixin_33674437 2018-01-27 17:13
    关注

    You should use an Ajax request in your function intilize(). Link your request to your PHP script, pass your vars (location, location1 and location2) into the data parameter of the Ajax request, and handle them into your PHP script to save them in your database.

    Something like that:

    $.ajax({
       url: '/script.php',  // Your script
       type: 'POST',
       dataType: 'json',
       data: { location: location, location1: location1, location2: location2 }
    });
    

    Then, in script.php, you will be able to access location, location1 and location2 by using $_POST["location"].

    评论

报告相同问题?

悬赏问题

  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)
  • ¥20 matlab yalmip kkt 双层优化问题
  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体
  • ¥88 实在没有想法,需要个思路
  • ¥15 MATLAB报错输入参数太多
  • ¥15 python中合并修改日期相同的CSV文件并按照修改日期的名字命名文件