【问题标题】:PHP/MySQL: Saving user input to DatabasePHP/MySQL:将用户输入保存到数据库
【发布时间】:2015-04-25 08:38:04
【问题描述】:

我有一个页面,它显示了一个地图,用户可以在其中添加标记并显示一些输入框供他们添加信息(名称、描述等)。然后将该信息发送到数据库,在必要时可以在该数据库中调用和显示。

单击“保存”时,该框应关闭并显示一条消息(“完成”)

将其发送到数据库的 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>

【问题讨论】:

  • JS 控制台有错误信息吗?
  • 很抱歉成为“那个人”。您可能想研究 PDO 之类的东西。您的 mysql 方法已经过时并且存在安全风险。 php.net/manual/en/function.mysql-select-db.php
  • 你将mysql_与PDO混合
  • 删除@符号以查看被丢弃的警告/错误
  • 点击保存时报错:“Uncaught ReferenceError: trace is not defined (anonymous function) jQuery.event.dispatch jQuery.event.add.elemData.handle”

标签: javascript php jquery mysql google-maps-api-3


【解决方案1】:

您的代码(来自教程)使用函数 'trace'

trace(data);

但是这个函数没有定义。 在教程演示中,此函数定义为:

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

尝试将此代码添加到您的脚本中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-05-21
    • 2011-04-01
    • 2012-09-29
    • 2021-01-29
    • 1970-01-01
    • 2014-11-19
    • 2014-06-22
    • 1970-01-01
    相关资源
    最近更新 更多