簡體   English   中英

PHP / MySQL:將用戶輸入保存到數據庫

[英]PHP/MySQL: Saving user input to Database

我有一個頁面,該頁面顯示了一個地圖,用戶可以在其中添加標記,並顯示一些輸入框供他們添加信息(名稱,說明等)。 然后將該信息發送到數據庫,在需要時可以在數據庫中調用和顯示該信息。

單擊“保存”后,該框應關閉並顯示一條消息(“完成”)

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);
?>

單擊“保存”按鈕后,什么也不會發生,該窗體保持打開狀態並帶有文本輸入,並且不會保存到DB。 我在瀏覽器上運行了“ 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>

您的代碼(來自教程)使用函數“ trace”

trace(data);

但此功能未定義。 在教程演示中,此函數定義為:

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

嘗試在您的腳本中添加此代碼。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM