【问题标题】:How to correctly select and show (or insert) data from (in) MySQL database with AJAX call to PHP function?如何使用 AJAX 调用 PHP 函数从 MySQL 数据库中正确选择和显示(或插入)数据?
【发布时间】:2013-02-09 22:32:16
【问题描述】:

到目前为止,我有这样的事情:

//HTML
<body onload=getRoomTemp();>

//JS
function getRoomTemp() {
$.ajax({
    type: "POST",
    url: "getRoomTemp.php",
    datatype: "text";
    data: {
        temp: temp
    }
}).done(function () { $('#getRoomTemp').append(text); });
}

//PHP
<?php
if (isset($_POST['temp'])) {
require('database.php');

$query = ("SELECT temp FROM tempWHERE tempID=1");

$res = mysql_query($query) or die("ERROR ".__LINE__.": ".mysql_error());
while ($ar = mysql_fetch_array($res)) {
    $temperatureIn = $ar['temp'];
    echo $temperatureIn;
    }
}
?>

所以,当我的 HTML 正文加载时,我想使用 AJAX 进行查询并在名为“getRoomTemp”的 div 中显示查询结果。稍后,我将需要相同的技术在单击按钮时在 MySQL 中插入数据(单个数值)。

我找不到当前代码的问题,为 ajax 尝试了不同的 dataType 但没有成功。请帮忙..

【问题讨论】:

    标签: php mysql ajax function jquery


    【解决方案1】:

    你有两个未定义的标识符temp和text,试试

    function getRoomTemp() {
      $.ajax({
        type: "POST",
        url: "getRoomTemp.php",
        dataType: "text",
        data: {
            temp: 'temp'
        }
      }).done(function (text) { $('#getRoomTemp').append(text); });
    }
    

    【讨论】:

    • 在datatype前面把;改成, :)
    • 嗯,谢谢大家,我已经做到了,现在什么也没发生,谷歌浏览器的控制台说:Uncaught TypeError: Illegal invocation in jQuery library :(
    猜你喜欢
    • 2015-05-12
    • 1970-01-01
    • 2016-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多