ajax json响应数组在php中使用 [英] ajax json response array using in php

查看:86
本文介绍了ajax json响应数组在php中使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用json响应制作了一个ajax表单。 json数组包含mysql数据库中的信息。现在我想在表格中显示这些数据。

I made an ajax form with json response. The json array contains information out of a mysql database. Now I want to show these datas in a table.

我在隐藏的html文件中创建了一个占位符。

I made a placeholder in the html file which is hidden.

这里我的代码为ajax / json部分:

Here my Code for the ajax/json part:

$("#select_coffee_talk_year").button().click(function() {
    var form = $('#coffee_talk_year');  
    var data = form.serialize();

    $.ajax({
        url: "include/scripts/select_event.php",
        type: "POST",
        data: data,
        dataType: 'json',
        success: function (select) {
            //alert(select.ID[0]);
            //alert(select.ID[1]);
            //alert(select.ID.length);


            $("#coffee_talk").fadeOut();
            $("#coffee_talk").fadeIn();
        }   
    });
    return false;
});

这是我的html:

<p class="bold underline headline">Bereits eingetragen:</p>
    <form id="coffee_talk_year" action="include/scripts/select_event.php" method="post" accept-charset="utf-8"> 
        <select name="year_coffee_talk" id="year_coffee_talk">
            <option value="none" class="bold italic">Jahr</option>
            <?php
                for($i=2008; $i<=$year; $i++){
                    if ($i == $year) {
                        echo "<option value=\"".$i."\" selected=\"$i\">".$i."</option>\n";
                    } else  echo "<option value=\"".$i."\">".$i."</option>\n";
                }   
            ?>
        </select>
        &nbsp;&nbsp;
        <button id="select_coffee_talk_year">anzeigen</button>
        <input type="hidden" name="coffee_talk_year_submit" value="true" />​​​​​​​​​​​​​​​​​
    </form>
    <br />
    <div id="coffee_talk"></div>
    <br />
    <button id="add_coffee_talk">hinzufügen</button>

select_event.php:

select_event.php:

if ('POST' == $_SERVER['REQUEST_METHOD']) {
    /*******************************/
    /** Erzaehlcafe auswählen
    /*******************************/
    if (isset($_POST['coffee_talk_year_submit'])) {
        $getID = array();
        $getDate = array();
        $getTheme = array();
        $getContributer = array();
        $getBegin = array();
        $getPlace = array();
        $getEntrance = array();
        $getFlyer = array();

        $sql = "SELECT 
                    ID,
                    Date,
                    Theme,
                    Contributer,
                    Begin,
                    Place,
                    Entrance,
                    Flyer
                FROM 
                    Coffee_talk
                WHERE
                    YEAR(Date) = '".mysqli_real_escape_string($db, $_POST['year_coffee_talk'])."'
                ";

        if (!$result = $db->query($sql)) {
            return $db->error;
        }

        while ($row = $result->fetch_assoc()) {
            $getID[$i] = $row['ID'];
            $getDate[$i] = $row['Date'];
            $getTheme[$i] = $row['Theme'];
            $getContributer[$i] = $row['Contributer'];
            $getBegin[$i] = $row['Begin'];
            $getPlace[$i] = $row['Place'];
            $getEntrance[$i] = $row['Entrance'];
            $getFlyer[$i] = $row['Flyer'];
            $i++;
        }

        $result->close();

        $response['ID'] = $getID;
        $response['Date'] = $getDate;
        $response['Theme'] = $getTheme;
        $response['Contributer'] = $getContributer;
        $response['Begin'] = $getBegin;
        $response['Place'] = $getPlace;
        $response['Entrance'] = $getEntrance;
        $response['Flyer'] = $getFlyer;

        echo json_encode($response);
    }
}

div = id = coffee_talk是我的占位符。现在我希望用它的数据淡出表格,如果我改变年份并用按钮提交它我希望淡出旧的那个并且淡入新的。

Div with id=coffee_talk is my placeholder. Now I wish to fade in the table with its data and if I change the year and submit it with the button I wish to fade the old one out and fade new in.

我唯一的问题是我需要在php中用循环编写这个表。但我认为它在Java Script中是不可能的。我该怎么办?

My only problem is that I need to write this table in php with loops. But I think its not possible in Java Script. What should I do?

PS我使用了ajax因为我不想一直重装。

PS I used ajax cause I dont want to have a reload all the time.

推荐答案

您的快速解决方案是:

$("#select_coffee_talk_year").button().click(function() {
    var form = $('#coffee_talk_year');  
    var data = form.serialize();

    $.ajax({
        url: "include/scripts/select_event.php",
        type: "POST",
        data: data,
        dataType: 'json',
        success: function (select) {
            var coffee_talk = $("#coffee_talk");
            coffee_talk.fadeOut('fast', function() {
                for(i in select) {
                    row = select[i];
                    div = coffee_talk.append('<div id="row_'+i+'" />');
                    for(column in row) {
                       div.append('<span class="column_'+column+'">'+row[column]+'</span>');
                    }
                }
                coffee_talk.fadeIn();
            });
        }   
    });
    return false;
});

为了更好的方法,你应该查找 Moustache.js 这是一个客户端side JavaScript模板引擎(在PHP / Java / Ruby / Python / Go和其他语言中具有等价物,基于Google CTemplates)。

For a nicer approach you should lookup Moustache.js which is a client side JavaScript templating engine (which has equivalents in PHP/Java/Ruby/Python/Go and other languages and is based on Google CTemplates).

它将允许您创建HTML模板并使用变量中的数据填充它们,例如AJAX请求可能收到的JSON变量。

It will allow you to create HTML templates and populate them with the data you have in a variable such as the JSON variable an AJAX request might receive.

这篇关于ajax json响应数组在php中使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆