2015-02-23 104 views
0

我有一個頁面,它顯示了一個地圖,用戶可以在其中添加一個標記併爲其添加信息(名稱,描述等)。然後這個信息被髮送到數據庫,在那裏它可以被召回並在必要時顯示。PHP/MySQL:將用戶輸入保存到數據庫中

當點擊SAVE,盒子應關閉和顯示消息( 「完成」)

的JS函數它發送到數據庫:

//when user clicks on the "submit" button 
     form.submit({point: point}, function (event) { 
     //prevent the default form behavior (which would refresh the page) 
     event.preventDefault(); 

     //put all form elements in a "data" object 
     var data = { 
      name: $("input[name=name]", this).val(), 
      description: $("textarea[name=description]", this).val(), 
      category: $("select[name=category]",this).val(), 
      lat: event.data.point.overlay.getPosition().lat(), 
      lon: event.data.point.overlay.getPosition().lng() 
     }; 
     trace(data) 

     //send the results to the PHP script that adds the point to the database 
     $.post("adddata.php", data, tidy_maps.saveStopResponse, "json"); 

     //Erase the form and replace with new message 
     infowindow.setContent('done') 
     return false; 

在 'adddata.php':

<?php 
ini_set("display_errors",1); 
//database access info 
$db = new mysqli("localhost", "stephen", "pass1", "gmaps1"); 

$statement = $db->stmt_init(); 

//database insert statement 
$statement->prepare("INSERT INTO tidy_maps_test (lat, lon, name, description, category) VALUES (?, ?, ?, ?, ?)"); 

//grab values from the url and add to database 
$statement->bind_param("ddsss", &$_POST['lat'], &$_POST['lon'],  &$_POST['name'], &$_POST['description'], &$_POST['category']); 
$status = $statement->execute(); 

//create a status message 
if ($status) 
{ 
    $message = array("message" => $_POST['name'] . " has been added!"); 
} 
else 
{ 
    $message = array("error" => $db->error); 
} 

$statement->close(); 

echo json_encode($message); 
?> 

當單擊保存按鈕時,什麼也沒有發生,窗體保持打開與文本輸入並不保存到數據庫。我在瀏覽器上運行'adddata.php'來查看它是否連接到數據庫,並且它是。

我一直在使用本教程作爲指南:http://gis.yohman.com/up206b/tutorials/9-2/


我試着改變按鈕輸入,但仍然沒有運氣。我的HTML的形式是:

<form class="form-horizontal save-form" style="display: none"> 
     <h1>Add me!</h1> 
     <fieldset> 
      <div class="control-group"> 
       .... 
       Form Contents Here 
       ... 
       </div> 
      </div> 
      <div class="form-actions">      
       <input name="Save" type="submit" class="btn btn-primary" value="Save"> 
      </div> 
     </fieldset> 
    </form> 
+0

JS控制檯中的任何錯誤消息? – 2015-02-23 21:24:07

+1

對不起,「那個傢伙」。你可能想看看PDO。你的mysql方法已經過時了,並且存在安全風險。 http://php.net/manual/en/function.mysql-select-db.php – FunkyMonk91 2015-02-23 21:24:30

+0

你的混合mysql_與PDO – 2015-02-23 21:41:00

回答

1

你的代碼(從教程)使用功能 '追蹤'

trace(data); 

,但沒有定義的功能。 在教程演示該功能定義爲:

function trace(message) 
{ 
    if (typeof console != 'undefined') 
    { 
     console.log(message); 
    } 
} 

嘗試在你的腳本中添加該代碼。

相關問題