如何让AJAX显示成功函数并允许页面更新数据库结果


How to get AJAX to show a success function and allow the page to update database results

我有以下代码,我不知道如何允许我的AJAX调用发送到我的PHP文件,然后允许我的页面在不提交表单的情况下显示页面上的更改。我需要页面不重新加载的原因是允许显示成功消息。

我想要做的是批准一个用户,一旦他们被批准,他们的名字将显示在页面的不同部分,然后我想要成功的消息显示更改后。

到目前为止,一切都在我的数据库和status变化中工作。此外,成功消息显示在它应该出现的地方,但用户名没有移动,直到我重新加载页面。

我怎样才能让所有这些工作而不重新加载页面?

if( $numrows ) {
        while($row = mysqli_fetch_assoc($run)){
            if($row['status'] == "Pending"){
                $pending_id        = $row['id'];
                $pending_user_id   = $row['user_id'];
                $pending_firstname = $row['firstname'];
                $pending_lastname  = $row['lastname'];
                $pending_username  = $row['username'];
                $pending_email  = $row['email'];
?>
        <form action="" method="POST" id="status">
             <input type='hidden' name='id' value='<?php echo $pending_id; ?>' id='pending_id'/>
<?php
        if ($pending_firstname == true) {
            echo "Name - ". $pending_firstname . " " . $pending_lastname . "</br>" . 
                "Username - ". $pending_username . "</br></br>"
?>

            <button class="approve" type="submit" form="status" name="approve" value="<?=$pending_id;?>">Approve</button>
            <button class="deny" type="submit" form="status" name="deny" value="<?=$pending_id;?>">Deny</button>
        </form>
AJAX

 $('.approve').click(function () {
        $.ajax({
            url: 'userRequest_approve.php',
            type: 'POST',
            data: {
                id: $(this).val(), //id
                status: 'Approved' //status
            },
            success: function (data) {
                //do something with the data that got returned
                $("#success").fadeIn();
                $("#success").show();
                $('#success').html('User Status Changed!');
                $('#success').delay(5000).fadeOut(400);
            },
            //type: 'POST'
        });
        return false;
    });

更新显示输出数据

<h2>Approved User Requests</h2><br>
    <div id="success" style="color: red;"></div><br>
$run2 = mysqli_query($con2,"SELECT * FROM user_requests ORDER BY id DESC");
$runUsers2 = mysqli_query($con2,"SELECT * FROM users ORDER BY id DESC");
$numrows2 = mysqli_num_rows($run2);
    if( $numrows2 ) {
        while($row2 = mysqli_fetch_assoc($run2)){
            if($row2['status'] == "Approved"){
            //var_dump ($row2);
                $approved_id        = $row2['user_id'];
                $approved_firstname = $row2['firstname'];
                $approved_lastname  = $row2['lastname'];
                $approved_username  = $row2['username'];
                $approved_email  = $row2['email'];
    if ($approved_firstname == true) {
        echo "Name - ". $approved_firstname . " " . $approved_lastname . "</br>" . 
            "Username - ". $approved_username . "</br></br>"

是否与您从ajax查询和消息成功调用的页面相同?

你应该在你的php文件中使用json,然后你可以检查你在回调中添加的任何值,像这样:

userRequest_approve.php

<?php
header('Content-type: application/json');
echo '[{"success":1,"username","theusername"}]';
?>

script.js

$('.approve').click(function(){
    $.post('userRequest_approve.php',$('#status').serialize(),function(data){
        alert(data[0].success+' username='+data[0].username);
    },'json');
    return false;
});