Bootstrap Ajax模式 - 显示正文和页脚的问题

时间:2015-05-12 12:28:37

标签: javascript jquery ajax twitter-bootstrap

我试图显示一个Ajax模式,它将在提交时显示数据库值。但是,我似乎无法通过网址显示信息。我是AJAX的新手,所以这可能是一个非常简单的问题。

我使用此模式调用模态(表格中的多个)

<a class="btn btn-small btn-primary" data-toggle="modal" data-target="#exampleModal" data-whatever="<?php echo $Name = $row['Name']; ?>">Read More</a>

模态如下:

<!-- Modal -->
<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="memberModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
                <h4 class="modal-title" id="memberModalLabel">Member Detail</h4>
            </div>
            <div class="dash">

            </div>

        </div>
    </div>
</div>

和AJAX

$('#exampleModal').on('show.bs.modal', function (event) {
      var button = $(event.relatedTarget) // Button that triggered the modal
      var recipient = button.data('whatever') // Extract info from data-* attributes
      var modal = $(this);
      var dataString = recipient;

        $.ajax({
            type: "GET",
            url: "getEventDetails.php",
            data: dataString,
            cache: false,
            success: function (data) {
                console.log(data);
                modal.find('.ct').html(data);
            },
            error: function(err) {
                console.log(err);
            }
        });  
})

最后是AJAX中的模态:

    $name = $_GET['name'];  

    //executes the SQL query
    $result = mysql_query("SELECT * FROM Events WHERE Name = '$Name'");

    if (isset($_POST)) {
        while ($row = mysql_fetch_array($result)) {
            $Date = $row['Date'];
            $Name = $row['Name'];
            $Type = $row['Type'];
            $Region = $row['Region'];
        }
    }  
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <!--Style Sheets-->
    <link href ="css/bootstrap.min.css" rel="stylesheet">
    <link href ="css/style.css" rel="stylesheet">
    <link href ="css/font-awesome.css" rel="stylesheet">
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Using Bootstrap modal</title>
</head>
<body>

    <div class="modal-body">             
        <?php echo $name ?> 
    </div>
    <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
    </div>

</body>
</html>

1 个答案:

答案 0 :(得分:1)

好吧,看起来在进行ajax调用时出现问题。如果我们查看“检查员网络”选项卡,我们可以看到您对服务器的请求已取消:

enter image description here

进一步查看控制台会显示错误:

enter image description here

我从这里可以看到,您的主站点设置为通过HTTPS工作,但您所做的请求并不安全。只是为了进行故障排除,请尝试添加HTTPS作为协议的文件的完整路径。

  $.ajax({
        type: "GET",
        url: "/getEventDetails.php/",
        data: dataString,
        cache: false,
        success: function (data) {
            console.log(data);
            $('.dash').html(data);
        },
        error: function(err) {
            console.log(err);
        }
    }); 

后续

如果您查看从var_dump($_GET);获得的内容,您会发现输出结果并不正确。

array(2) {
    ["Cardiff_Charity_Race"]=> string(0) "" 
    ["_"]=> string(13) "1431438472999" 
}

你可以看到钥匙&#39; Cardiff_Charity_Race&#39;等于&#39;&#39;&#39; _&#39;等于&#39; 1431438472999&#39;。这不是我们想要的。我们想要&#39; name&#39;等于&#39; Cardiff_Charity_Race&#39;像:

array(1) {
    ["name"]=> string(0) "Cardiff Charity Race" 
}

为此,请尝试正确格式化dataString,类似于:

$('#exampleModal').on('show.bs.modal', function (event) {
      var button = $(event.relatedTarget) // Button that triggered the modal
      var recipient = button.data('whatever') // Extract info from data-* attributes
      var modal = $(this);
      var dataString = 'name=' + recipient;
      console.log(dataString);
        $.ajax({
            type: "GET",
            url: "getEventDetails.php",
            data: dataString,
            cache: false,
            success: function (data) {
                console.log(data);
                modal.find('.ct').html(data);
            },
            error: function(err) {
                console.log(err);
            }
        });  
});