简体   繁体   English

PHP / MySQL:将用户输入保存到数据库

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

I have a page which brings up a map where the user can add a marker and brings up a few input boxes for them to add info (name, description, etc.). 我有一个页面,该页面显示了一个地图,用户可以在其中添加标记,并显示一些输入框供他们添加信息(名称,说明等)。 This info is then to be sent to a database where it can be recalled and displayed where necessary. 然后将该信息发送到数据库,在需要时可以在数据库中调用和显示该信息。

When SAVE is clicked, the box should close and display a message ("Done") 单击“保存”后,该框应关闭并显示一条消息(“完成”)

The JS function which sends it 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;

The 'adddata.php': '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);
?>

When the SAVE button is clicked, nothing happens, the form stays open with the text input and does not save to DB. 单击“保存”按钮后,什么也不会发生,该窗体保持打开状态并带有文本输入,并且不会保存到DB。 I ran the 'adddata.php' on my browser to see if it is connected to the database and it is. 我在浏览器上运行了“ adddata.php”,以查看它是否已连接到数据库。

I've been using this tutorial as a guide : http://gis.yohman.com/up206b/tutorials/9-2/ 我一直在使用本教程作为指南: http : //gis.yohman.com/up206b/tutorials/9-2/


I've tried changing the button to an input but still no luck. 我尝试将按钮更改为输入,但仍然没有运气。 My HTML form is: 我的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>

Your code (from tutorial) uses function 'trace' 您的代码(来自教程)使用函数“ trace”

trace(data);

but this function is not defined. 但此功能未定义。 In tutorial demo this function defined as: 在教程演示中,此函数定义为:

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

Try to add this code in your script. 尝试在您的脚本中添加此代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM