更改数据库

时间:2015-07-20 15:57:37

标签: php jquery html ajax

我有一个页面,用户可以选择设计' A'或者' B'等等。点击'点击'用Jquery / AJAX制作。 我有一个foreach来监听用户选择的内容,并相应地输出到div。

我的问题是:我怎样才能做到这一点,只有正在改变的内容会立即刷新,因此用户可以轻松地在设计之间切换。 ; A',' B'或者' C'?

这就是我现在刷新整个网站的内容......

$("#presetchange2").on('click', function() {
    $.ajax({
        url: "includes/presethandler2.php",
        success: function(){
            window.location.reload(true);
        }
    });
});

PHP我使用

<?php
$query = "
        SELECT
                dashboard_presets.includes AS includes
                FROM dashboard
                INNER JOIN dashboard_presets
                ON dashboard.preset = dashboard_presets.preset
                WHERE user_id = $user_id
    ";
try
{
    // Runs query to DB
    $stmt = $db->prepare($query);
    $stmt->execute();
}
catch(PDOException $ex)
{
    die("Failed to run query: " . $ex->getMessage());
}
$rows = $stmt->fetchAll();
?>

<?php foreach($rows as $row): ?>
   <div class="col-md-4 col-sm-6">
      <?php echo $row['includes']; ?>
   </div>
<?php endforeach;?>

1 个答案:

答案 0 :(得分:0)

如果你让你的服务器只返回部分标记,你需要替换预设,你可以这样做:

$("#presetchange2").on('click', function() {
    $.ajax({
        url: "includes/presethandler2.php",
        success: function(html){
            $('#targetElement').html(html);
        }
    });
});

其中targetElement是您要填充的元素的ID。