weixin_33709364 2015-04-06 21:58 采纳率: 0%
浏览 48

在AJAX成功上重新加载div

I have a div which has PHP code (it prints data from a database). I made an AJAX function to send the data to the server without refreshing the page, here's the code:

var url = "loadsave.php";

   $('.create_save').submit(function(event) {
        if ($('.gss_new_save').val()==""){
            event.preventDefault();     
        } else {
            $.ajax({
                    type: "POST",
                    url: url,
                    data: $('form').serialize(),
                    success: function()
                    {

                    }
                });
            event.preventDefault();
        }
    });

And this is the other part that prints the code. The problem is that it prints the code just when you refresh the page:

<?php
            $connect = new PDO('mysql:host=localhost;dbname=game','root','');
            $cmd = $connect->query('SELECT * FROM saves');
            $result = $cmd -> fetchAll(PDO::FETCH_ASSOC);

            foreach ($result as $res) {
                ?>
                <div class="gss_save cls">
                    <span class="gss_save_text cls"><?php echo $res['name'];?></span>
                    <span class="gss_level">lvl: <?php echo $res['level'];?></span>
                </div>
                <?php
            }
        ?>

EDIT: I add: the form:

<form method="post" action="loadsave.php" class="create_save">
            <input type="text" class="gss_new_save" name="name">
            <input type="submit" style="display:none">
        </form> 

and the content of loadsave.php:

<?php
        if (isset($_POST['name'])) {
            $nombre = $_POST['name'];   
        }

        $connect = new PDO('mysql:host=localhost;dbname=game','root','');

        $stm = $connect->query("SELECT * FROM saves WHERE `name` ='$nombre'");
        $result = $stm->fetchAll(PDO::FETCH_ASSOC);

        if(count($result)==0){
            $cmd = $connect->prepare('INSERT INTO saves (name, level) VALUES (:nombre,"1")');
            $cmd -> bindParam(":nombre",$nombre);
            $cmd -> execute();
        }
    ?>

It works like a charm but I want to refresh the div to print the content I just sent, how can I achieve this?

  • 写回答

1条回答 默认 最新

  • weixin_33695082 2015-04-06 22:01
    关注

    Assuming the script echoes the new content back:

    success:function(response) {
        $("#divID").html(response);
    }
    

    loadsave.php should be:

    <?php
        if (isset($_POST['name'])) {
            $nombre = $_POST['name'];   
        }
    
        $connect = new PDO('mysql:host=localhost;dbname=game','root','');
    
        $stm = $connect->query("SELECT * FROM saves WHERE `name` ='$nombre'");
        $result = $stm->fetchAll(PDO::FETCH_ASSOC);
    
        if(count($result)==0){
            $cmd = $connect->prepare('INSERT INTO saves (name, level) VALUES (:nombre,"1")');
            $cmd -> bindParam(":nombre",$nombre);
            $cmd -> execute();
        }
    
        $cmd = $connect->query('SELECT * FROM saves');
        $result = $cmd -> fetchAll(PDO::FETCH_ASSOC);
    
        foreach ($result as $res) {
            ?>
            <div class="gss_save cls">
                <span class="gss_save_text cls"><?php echo $res['name'];?></span>
                <span class="gss_level">lvl: <?php echo $res['level'];?></span>
            </div>
            <?php
        }
    
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大