javascript - 无需页面刷新即可制作 Ajax 和 PHP 更新页面?

标签 javascript php jquery sql twig

每当我提交“添加账单”表单时,在我刷新页面之前什么都没有发生。那是我在 Twig 循环中看到我的新项目的时候。当我点击删除链接时,同样的问题发生了。在我刷新页面之前,没有任何内容(在视觉上)被删除。

如何在不刷新页面的情况下立即在页面上执行这些操作?我认为这可能与我的 PHP 或 SQL 有关?

JavaScript:

$(document).ready(function() {
    $(".addBill").on("click", function() {
        var billAmount = $('.billAmount').val();
        var billName = $('.billName').val();
        $.ajax({
            type: "POST",
            url: "index.php",
            data: { 
                bill_amount: billAmount, 
                bill_name: billName,
                action: 'addBill'
            }
        });
        return false;
    });

    $(".removeBill").on("click", function() {
        var id = $(this).data('id');
        $.ajax({
            type: "POST",
            url: "index.php",
            data: { 
                id_to_delete: id, 
                action: 'removeBill' 
            }
        });
        return false;
    });
});

HTML:

<form method="post" name="addBillForm">
    <input type="text" placeholder="Enter bill name" name="billName" class="billName">
    <input type="text" placeholder="Enter bill amount" name="billAmount" class="billAmount">
    <input type="submit" value="Submit" name="addBillForm" class="addBill">
</form>

<br><br>
<h2>My Bills</h2>
{% for bill in bills %}
    <p>{{ bill.billName }} - {{ bill.billAmount }} - 
        <a href="#" class="removeBill" data-id="{{ bill.id }}">Remove</a>
    </p>
{% endfor %}

这是我的 PHP 文件:

<?php

require_once 'global.php';

if (@$_POST['action'] == 'addBill')
{
    $billName = $_POST['bill_name'];
    $billAmount = intval($_POST['bill_amount']);
    $stmt = $db->prepare("INSERT INTO bills (billName, billAmount) VALUES(?,?)");
    $stmt->bindParam(1, $billName);
    $stmt->bindParam(2, $billAmount);
    $stmt->execute();
}

if (@$_POST['action'] == 'removeBill')
{
    $id = intval($_POST['id_to_delete']);
    $stmt = $db->prepare("DELETE FROM bills WHERE id = ?");
    $stmt->bindValue(1, $id);  
    $stmt->execute();
}

$billResults = $db->query('SELECT * FROM bills');
$bills = $billResults->fetchAll(PDO::FETCH_ASSOC);

$twigContext = array(
    "bills" => $bills
);

echo $twig->render('base.html.twig', $twigContext);

最佳答案

在 AJAX 调用完成后,您实际上并未对页面执行任何操作。例如:

$.ajax({
    type: "POST",
    url: "index.php",
    data: { 
        bill_amount: billAmount, 
        bill_name: billName,
        action: 'addBill'
    },
    success: function (data) {
        // update the page somehow
    },
    error: function () {
        // there was an error, handle it here
    }
});

页面不会自动知道应该如何更新。您必须在该函数中编写代码才能执行此操作。可能通过识别一些页面元素并修改它们的内容,添加/删除其他页面元素等。

关于javascript - 无需页面刷新即可制作 Ajax 和 PHP 更新页面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34473231/

相关文章:

php - 将变量传递给 mysql 查询结果为 "function query on null"

php - 登录php后重定向到首页

javascript - 使用 HTML 将待办事项列表数据保存到本地存储

jquery - 非常简单的 "insert a word to continue"脚本

javascript - 如何在 jQuery 中将创建的 var 元素相互追加?

javascript - 使用 javascript 添加的表单元素不是从 post 方法发送的

php - 在 Woocommerce 购物车和结帐日期时间显示中启用用户本地时间

javascript - 从文本文件生成随机行

javascript - 两个 div 同一类,仅获取该 div 的标题

javascript - 在提交按钮上显示文本区域中的文本 - Angular JS