【问题标题】:with ajax post and show recived data使用 ajax 发布并显示接收到的数据
【发布时间】:2015-04-09 22:38:24
【问题描述】:

我写了这段代码,但它不起作用。我想使用 ajax 在 php 中显示一个数组。 这是一个 html 选择,它选择该选择列表值的每个选项,该选项的选项接受变量并将其发送到 ajax。然后 Ajax 应该将数据发布到 php,然后 php 从数据库中选择接收到的数据并显示所有这些数据。 但我无法在 ajax 中显示这些数据。 :(

$(function(){
    $("#topic").change(function(){
        var str = "";
        $( "select option:selected" ).each(function() {
            str += $( this ).text() + " ";
            options(str);
        });
    });
});

function options(option){
    $.ajax({
        type: "POST",
        dataType: 'json',
        url: "/Register/checkSelect", //Relative or absolute path to response.php file
        data: {
            option:option
        }).done(function(){
            $("#content").html(data);
            alert("ok");
        });
    });
}

【问题讨论】:

  • 你忘记发布你的 php 代码了。
  • 对不起,这是 PHP cod public function checkSelect(){ $getTopic= $_POST["topic"]; $db=新数据库(); $result= $db->query("SELECT * FROM topic WHERE topic='$getTopic'"); foreach ($result as $topic) { echo $topic['keyword']; } }
  • data 回调中没有 data 参数。 html() 也将全部替换。请使用浏览器控制台/开发工具检查错误
  • 我插入数据但不工作
  • 你能帮我写一个这样的样本吗

标签: php jquery ajax


【解决方案1】:

您的 ajax 请求中有错误。 这里是正确的代码:

function options(option){
        $.ajax({
                type: "POST",
                dataType: 'json',
                url: "/Register/checkSelect", //Relative or absolute path to response.php file
                data: {
                    option:option
                }
    }).done(function(data){
                 $("#content").html(data);
                 alert("ok");
   });
}

【讨论】:

  • 1-> 我将此数据发送到 php 2-> php 从 db 3 中选择数据-> 但我无法显示此数据。
  • 您收到警报了吗?如果是,能否打印数据对象:console.log(data)?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-09-13
  • 1970-01-01
  • 2013-08-21
  • 2012-04-03
  • 2013-02-14
  • 1970-01-01
  • 2016-10-06
相关资源
最近更新 更多