如何写入客户端

时间:2016-06-20 19:48:46

标签: javascript php mysql ajax

我想要一个Select Box,我可以从数据库中过滤数据(使用Ajax - 没有站点重新加载)。例如:按ID排序数据或按名称排序数据 这很好,现在当我得到那些数据时,我也希望我能用输入编辑数据并保存它(也是通过ajax),但我不知道如何。我知道如何使用ajax保存数据,但不知道数据是来自另一个ajax的响应。

代码:(这里格式不正确,这就是为什么使用pastebin:http://pastebin.com/7mgi6VMc

指数:

<div class="wrapper">
    <p id="result">Erfolgreich gespeichert!</p>
    <br>
    <div class="filter">
        <form>
            Anzeige: 
            <select class="selectFilter" onchange="filterFunction(this.value)">
                <option value="id_back">ID - Aufwärts</option>
                <option value="id_up">ID - Abwärts</option>
            </select>
        </form>
    </div>
    <br>
    <br>
    <div id="resultCatalogue">
        <b>Bitte wähle die gewünschte Anzeige ...</b>
    </div>
</div>

访问getdata.php:

<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="//fonts.googleapis.com/css?    family=Open+Sans" />
<script src="//code.jquery.com/jquery-1.10.2.min.js"></script>

<script>
$("#save").on("submit", function(event) {
    event.preventDefault();
    $.ajax({
        type: "POST",
        url: "./save.php",
        data: $(this).serialize(),
        success: function(data) {
            if($('#result').css('display') == 'none') {
                document.getElementById('result').style.display = 'block'
            } else {
                document.getElementById('result').style.display = 'none'
                setTimeout(function() {
            document.getElementById('result').style.display =     'block'
                }, 100);                
            }   
        },
    });
 });
</script>

</head>
<body>

<?php
$type = intval($_GET['type']);

if($type == "id_up") 
{
echo '
        <table class="tableCatalogue">
    <tr>
        <th class="th" width="5%">ID</th>
        <th class="th" width="5%">Unter-ID</th>
        <th class="th" width="15%">Titel</th>
        <th class="th" width="5%">Iceron</th>
        <th class="th" width="10%">Sichtbar</th>
        <th class="th" width="10%">Aktiv</th>
        <th class="th" width="5%">Min. Rank</th>
        <th class="th" width="5%">VIP</th>
        <th class="th" width="5%">Reihen Nummer</th>
        <th class="th" width="10%">Layout</th>
    </tr>
    <form method="post" id="save">
    ';

    $index = 0;
    $getCataloguePages = mysql_query("SELECT * FROM catalog_pages");
    while($row = mysql_fetch_array($getCataloguePages))
    {
        $id = $row['id'];
        $parent_id = $row['parent_id'];
        $caption = $row['caption'];
        $icon_image = $row['icon_image'];
        $visible = $row['visible'];
        $enabled = $row['enabled'];
        $min_rank = $row['min_rank'];
        $min_vip = $row['min_vip'];
        $order_num = $row['order_num'];
        $page_layout = $row['page_layout'];

        $i++;
        if(IsEven($i)){
            $even = "Even";
        } else {
            $even = "Odd";
        }
    echo '
    <tr>
        <td class="td'.$even.'" width="5%"><input placeholder="ID" class="inputTable" name="id[]" value="'.$id.'"></td>
        <td class="td<?php echo $even; ?>" width="5%"><input placeholder="Unter-ID" class="inputTable" name="parent_id[]" value="'.$parent_id.'"></td>
        <td class="td<?php echo $even; ?>" width="15%"><input placeholder="Titel" class="inputTable" name="caption[]" value="'.$caption.'"></td>
        <td class="td<?php echo $even; ?>" width="5%"><input placeholder="Icon" class="inputTable" name="icon_image[]" value="'.$icon_image.'"></td>
        <td class="td<?php echo $even; ?>" width="5%"><select placeholder="Sichtbar" class="selectTable" name="visible[]"><option value="1">Sichtbar</option><option value="0">Nicht sichtbar</option></select></td>
        <td class="td<?php echo $even; ?>" width="5%"><select placeholder="Aktiviert" class="selectTable" name="enabled[]"><option value="1">Aktiv</option><option value="0">Nicht aktiv</option></select></td>
        <td class="td<?php echo $even; ?>" width="5%"><select placeholder="Mindest Rank" class="selectTable" name="min_rank[]"><option>1</option></select></td>
        <td class="td<?php echo $even; ?>" width="5%"><select placeholder="VIP" class="selectTable" name="min_vip[]"><option value="1">Nur VIP</option><option value="0">Alle</option></select></td>
        <td class="td<?php echo $even; ?>" width="5%"><input placeholder="Reihen Nummer" class="inputTable" name="order_num[]" value="'.$order_num.'"></td>
        <td class="td<?php echo $even; ?>" width="10%"><input placeholder="Layout" class="inputTable" name="page_layout[]" value="'.$page_layout.'"></td>
    </tr>
    ';
    $index++;
    } 
    echo '
    <input class="save" type="submit" name="save" value="Speichern">
    <br><br>
    </form>
</table>
';

?>
<script>
$("#save").on("submit", function(event) {
event.preventDefault();
    $.ajax({
        type: "POST",
        url: "./save.php",
        data: $(this).serialize(),
        success: function(data) {
            if($('#result').css('display') == 'none') {
                document.getElementById('result').style.display = 'block'
            } else {
                document.getElementById('result').style.display = 'none'
                setTimeout(function() {
                    document.getElementById('result').style.display = 'block'
                }, 100);                
            }   
        },
    });
});
</script><?php
}

?>

save.php:

$size = count($_POST['id']); 
$i = 0; 
while ($i < $size) 
{
    $id = $_POST['id'][$i];         
    $query = "UPDATE catalog_pages SET id = '".$id."' LIMIT 1"; 
    mysql_query($query) or die ("Error in query: $query"); 
    ++$i; 
} 

就像我说的,一切都包括在内。数据库连接,一切都很好。 它还向我显示了Select的响应,但我想更新该数据 - 也使用ajax。

我在哪里放置Ajax代码?我怎么能这样做?

0 个答案:

没有答案