0

この質問をするのは私が初めてではないはずなので、このサイトを検索してみました。サイトに地図を配置し、ユーザーにマーカーと情報を追加してもらいたいと考えています。これは正常に機能していますが、情報が保存されていません。保存して閉じるボタンをクリックしても何も起こりません。どこが間違っているのか誰か教えてください。これがhtmlコードです

var マーカー; var 情報ウィンドウ;

function initialize() {
  var latlng = new google.maps.LatLng(-24.532226,31.290586);
  var options = {
    zoom: 8,
    center: latlng,
    mapTypeId: google.maps.MapTypeId.ROADMAP
  }
  var map = new google.maps.Map(document.getElementById("map-canvas"), options);
  var html = "<table>" +
             "<tr><td>Posted by:</td> <td><input type='text' id='name'/> </td> </tr>" +
             "<tr><td>Time +/-:</td> <td><input type='text' id='time'/></td> </tr>" +
             "<tr><td>Sighting:</td> <td><select id='sighting'>" +
             "<option value='Lion' SELECTED>Lion</option>" +
             "<option value='Rhino'>Rhino</option>" +
             "<option value='Elephant'>Elephant</option>" +
             "<option value='Buffalo'>Buffalo</option>" +
             "<option value='Leopard'>Leopard</option>" +
             "<option value='Cheetah'>Cheetah</option>" +
             "<option value='Spotted Hyena'>Spotted Hyena</option>" +
             "<option value='Wild Dogs'>Wild Dogs</option>" +
             "<option value='Hippo'>Hippo</option>" +
             "<option value='Kudu'>Kudu</option>" +
             "<option value='Zebra'>Zebra</option>" +
             "<option value='Fish Eagle'>Fish Eagle</option>" +
             "<option value='Other'>Other</option>" +
             "</select> </td></tr>" +
             "<tr><td>Description:</td> <td><input type='text' id='description'/> </td> </tr>" +
             "<tr><td></td><td><input type='button' value='Save & Close' onclick='saveData()'/></td></tr>";


infowindow = new google.maps.InfoWindow({
 content: html
});

google.maps.event.addListener(map, "click", function(event) {
    marker = new google.maps.Marker({
      position: event.latLng,
      map: map
    });
    google.maps.event.addListener(marker, "click", function() {
      infowindow.open(map, marker);
    });
});
}

function saveData() {
  var name = escape(document.getElementById("name").value);
  var time = escape(document.getElementById("time").value);
  var sighting = escape(document.getElementById("sighting").value);
  var description = escape(document.getElementById("description").value);

  var url = "phpsqlinfo_addrow.php?name=" + name + "&time=" + time +
  "&sighting=" + sighting + "&description" + description + "&lat=" + latlng.lat() + "&lng=" + latlng.lng();
  downloadUrl(url, function(data, responseCode) {
    if (responseCode == 200 && data.length <= 1) {
      infowindow.close();
      document.getElementById("msg").innerHTML = "Location added.";
    }
  });
}

function downloadUrl(url, callback) {
  var request = window.ActiveXObject ?
      new ActiveXObject('Microsoft.XMLHTTP') :
      new XMLHttpRequest;

  request.onreadystatechange = function() {
    if (request.readyState == 4) {
      request.onreadystatechange = doNothing;
      callback(request.responseText, request.status);
    }
  };

  request.open('GET', url, true);
  request.send(null);
}

function doNothing() {}
</script>

するとこれが体内で

<div id="map">
<body style="margin:0px; padding:0px;" onload="initialize()">
<div id="map-canvas" style="width: 950px; height: 534px"></div>
<div id="msg"></div>

そして、これは私のPHPです

    <?php
    require("phpsqlinfo_dbinfo.php");

    // Gets data from URL parameters
    $name = $_GET['map_client_name'];
    $time = $_GET['client_time'];
    $lat = $_GET['client_lat'];
    $lng = $_GET['client_lng'];
    $description = $_GET['client_description'];
    $sighting = $_GET['client_sighting'];


    // Opens a connection to a MySQL server
    $connection = mysql_connect ("mysql.spri.co.za", "sean_sql", "Paaseiers01");
    if (!$connection) {
    die('Not connected : ' . mysql_error());
    }


   // Set the active MySQL database
   $db_selected = mysql_select_db($kruger_park_live, $connection);
   if (!$db_selected) {
   die ('Can\'t use db : ' . mysql_error());
   }

   // Insert new row with user data
   $query = sprintf("INSERT INTO tbl_maps " .
         " (ID, map_client_name, client_time, client_lat, client_lng, client_description, 

   client_sighting) " .
         " VALUES (NULL, '%s', '%s', '%s', '%s', '%s', '%s');",
         mysql_real_escape_string($name),
         mysql_real_escape_string($time),
         mysql_real_escape_string($lat),
         mysql_real_escape_string($lng),
         mysql_real_escape_string($description),
         mysql_real_escape_string($sighting));

    $result = mysql_query($query);

    if (!$result) {
    die('Invalid query: ' . mysql_error());
    }

    ?>

誰かが私を助けてくれませんか、私はこれを4日間続けてきましたが、今はあきらめました. どうもありがとうございました

4

1 に答える 1

0

以下のコードは、jQueryを使用してデータを php ファイルに送信し、成功した場合はデータを返します。downloadUrl()

含む

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>

あなたの頭の中で

function saveData() {
     var latlng = marker.getPosition();
     var name = escape(document.getElementById("name").value);
     var time = escape(document.getElementById("time").value);
     var sighting = escape(document.getElementById("sighting").value);
     var description = escape(document.getElementById("description").value);
     $.get('phpsqlinfo_addrow.php',{lat:latlng.lat(),lng:latlng.lng(),name:name,time:time,sighting:sighting,description:description})
     .done(function(data){  
      alert("Data Loaded: " + data);
     });    
    }   

不要

function downloadUrl(url, callback)

function doNothing() {}

次のコードでは、非推奨の関数の代わりにPDOを使用していますmysql_

phpsqlinfo_addrow.php

require("dbinfo.php");// database connections
// Get parameters from URL
$lat = $_GET["lat"];
$lng = $_GET["lng"];
$name = $_GET["name"];
$time = $_GET["time"];
$description = $_GET["description"];
$sighting = $_GET["sighting"];
//Connect to database
$dbh = new PDO("mysql:host=$host;dbname=$database", $username, $password);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
try {
    // Prepare INSERT statement new sigting
        $stmt3 = $dbh->prepare("INSERT INTO tbl_maps (ID, map_client_name, client_time, client_lat, client_lng, client_description, client_sighting)  VALUES (NULL, ?, ?, ?, ?, ?, ?)");
        // Assign parameters
        $stmt3->bindParam(1,$name);
        $stmt3->bindParam(2,$time);
        $stmt3->bindParam(3,$lat);
        $stmt3->bindParam(4,$lng);
        $stmt3->bindParam(5,$description);
        $stmt3->bindParam(6,$sighting);
        $stmt3->execute();
        $data[] = array("name"=>$name, "lat"=>round($lat,6), "lng"=>round($lng,6),"time"=>$time,"description"=>$description,"sighting"=>$sighting);
        //Data for success
        echo json_encode($data);
}
catch(PDOException $e) {
    echo "Error Message.". $e->getMessage() ;// Remove or modify after testing 
    file_put_contents('PDOErrors.txt',date('[Y-m-d H:i:s]').", phpsqlinfo_addrow.php, ". $e->getMessage()."\r\n", FILE_APPEND);  
}
//Close the connection
$dbh = null; 
?>
于 2013-05-24T20:10:41.267 に答える