繁体   English   中英

使用Ajax将JS变量转换为同一php文件

[英]JS variables to same php file using ajax

选择文件时,我正在使用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进入表单?

请在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/

暂无
暂无

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

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