【问题标题】:JS variables to same php file using ajaxJS变量使用ajax到同一个php文件
【发布时间】:2017-04-15 13:10:04
【问题描述】:

当我选择一个文件时,我正在使用 jQuery 提取纬度变化

    document.getElementById("input-group").onchange = function(e)

    var liamlat = dec[1];

然后我使用 JSON 将它发送到同一个 php 文件

var dataString = JSON.stringify(liamlat);

        $.ajax({
           url: 'addnew.php',
           data: {lat: dataString},
           type: 'POST',
           success: function(response) {
              alert('lat added');
           }
        });

在 PHP 中,我使用 btn_submit 将 liamlat 发送到 mysql,这是我单击时的按钮 “提交”

require_once 'dbconfig.php';


if(isset($_POST['btn_submit'])){



$username = $_POST['userName'];
$watervisibility = $_POST ['waterVisibility'];
$divinglocation = $_POST['divingLocation'];
$glat = json_decode($_POST['lat']);

在我的 HTML 中,我使用隐藏的表单来回显它

 <input class="form-control" type="text" name="latform" placeholder="Lat" value="<?php echo $glat->var; ?>" />

现在我希望将来自 liamlat 的数据插入到我的数据库中。

数据没有通过,我收到回复说:

带有消息“SQLSTATE[23000] 的未捕获异常“PDOException”:违反完整性约束:1048 列“lat”不能为空

不知道为什么数据没有经过json进入表单?

【问题讨论】:

    标签: php jquery mysql json ajax


    【解决方案1】:

    请添加更多数据以在 ajax 函数中发布数据;

    $.ajax({
           url: 'addnew.php',
           data: {
                lat: dataString,
                btn_submit: 1,
                userName: 'username',
                waterVisibility: 'waterVisibility',
                divingLocation: 'divingLocation'
            },
           type: 'POST',
           success: function(response) {
              alert('lat added');
           }
    });
    

    请先阅读有关 ajax 的文档。:

    https://www.sitepoint.com/use-jquerys-ajax-function/

    http://api.jquery.com/jQuery.ajax/

    【讨论】:

    • 谢谢。不过,我不得不采用不同的方法。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-04-08
    • 2017-07-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多