Ajax 操作后刷新模态

Refresh modal after Ajax action

执行完 AJAX 操作后,我想刷新我的模式,但我不知道如何刷新。我在这个问题上花了太多时间。

在页面 ViewCharacter.php 中,您会看到一个包含最新数据的页面,您可以单击铅笔按钮“#ButtonEditCharacterStats”来编辑统计数据,然后它会打开一个为您提供最新数据的模态。当您单击按钮 "plus 1" 按钮 (#StrengthPlusOne) 时,它会将该数字递增 1,但之后我想关闭模态,再次加载它并显示最新数据。

或者更好的解决方案是保持模态打开并加载模态的内容。我似乎找不到解决方案。

任何进一步改进此脚本的想法将不胜感激。

ViewCharacter.php

<?php
$Character = new Character;
$Character->getSingleCharacter();

$result = $Character->getSingleCharacter();
?>

<h2>Character</h2>

<?php
while ($Character = $result->fetch_object())
{
    ?>
    <h3><?php echo $Character->FirstName ?></h3>
    </br>

    <div class="row">
        <div class="col-xs-6">
            <div class="panel panel-default">
                <div class="panel-heading">
                    Stats
                </div>
                <div class="panel-body"> 


                    <ul class="nav nav-pills" role="tablist">
                        <li role="presentation" class="active">
                            <button id="ButtonEditCharacterStats" style="" data-toggle="modal" data-target="#EditCharacterStats" class="ToolbarButton glyphicon glyphicon-pencil fa-2x"></button>

                            </br></br>

                            <a href="#">Strength <span class="badge"><?php echo $Character->Strength; ?></span><span class="badge"><?php echo $Character->StrengthBonus; ?></span></a></br>
                            <a href="#">Dexterity <span class="badge"><?php echo $Character->Dexterity; ?></span><span class="badge"><?php echo $Character->DexterityBonus; ?></span></a></br>
                            <a href="#">Constitution <span class="badge"><?php echo $Character->Constitution; ?></span><span class="badge"><?php echo $Character->ConstitutionBonus; ?></span></a></br>
                            <a href="#">Intelligence <span class="badge"><?php echo $Character->Intelligence; ?></span><span class="badge"><?php echo $Character->IntelligenceBonus; ?></span></a></br>
                            <a href="#">Wisdom <span class="badge"><?php echo $Character->Wisdom; ?></span><span class="badge"><?php echo $Character->WisdomBonus; ?></span></a></br>
                            <a href="#">Charisma <span class="badge"><?php echo $Character->Charisma; ?></span><span class="badge"><?php echo $Character->CharismaBonus; ?></span></a></br>

                        </li>
                    </ul>                
                </div>
                <?php
            }
            ?>

            <div class="panel-footer">
                Stats
            </div>
        </div>
    </div>
    <div class="col-xs-6">
        <div class="panel panel-default">
            <div class="panel-heading" style="background-color: #d9534f;color:white;">
                Stats
            </div>
            <div class="panel-body">
                <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum tincidunt est vitae ultrices accumsan. Aliquam ornare lacus adipiscing, posuere lectus et, fringilla augue.</p>
            </div>
            <div class="panel-footer">
                Stats
            </div>
        </div>
    </div>

</div>

<script>
    $(document).ready(function () {

        //$("#StrengthPlusOne").click(function()
        $("#StrengthPlusOne").on("click", function ()
        {
            $.ajax({
                method: "POST",
                url: "inc/view/Character/UpdateCharacterStat.php",
                data: {
                    ID: "2",
                    stat: "Strength",
                    Operator: "Increase"

                }
            })
                    .done(function (msg) {
                        alert("Data Saved: " + msg);
                    });
        });
    });

    $("#StrengthMinusOne").click(function () {
        alert("-1");
    });


</script>    

EditCharacterModal.php

<?php
$Character = new Character;
$Character->getSingleCharacter();

$result = $Character->getSingleCharacter();

while ($Character = $result->fetch_object()){?>
</br>

<div id="EditCharacterStats" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel" aria-hidden="true">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">
            <div class="panel-body">
                <div id="EditCharacterStatsContent">
                <h3>Edit Character Stats</h3>
                <ul class="nav nav-pills" role="tablist">
                    <li role="presentation" class="active">
                        </br></br>
                        <a href="#">
                            Strength 
                            <span class="badge"><?php echo $Character->Strength; ?>
                            </span> 
                        <button id="StrengthPlusOne" class="SmallToolbarButton glyphicon glyphicon-plus-sign fa-1x"> 1</button>
                        <button id="StrengthMinusOne" class="SmallToolbarButton glyphicon glyphicon-minus-sign fa-1x"> 1</button>

                        </a>


                        </br>
                        <a href="#">
                            Strength Bonus
                            <span class="badge"><?php echo $Character->StrengthBonus; ?></span>
                        </a></br>
                        <a href="#">
                            Dexterity 
                            <span class="badge"><?php echo $Character->Dexterity; ?></span>
                            <span class="badge"><?php echo $Character->DexterityBonus; ?></span>
                        </a></br>
                        <a href="#">
                            Constitution 
                            <span class="badge"><?php echo $Character->Constitution; ?></span>
                            <span class="badge"><?php echo $Character->ConstitutionBonus; ?></span>
                        </a></br>
                        <a href="#">Intelligence 
                            <span class="badge"><?php echo $Character->Intelligence; ?></span>
                            <span class="badge"><?php echo $Character->IntelligenceBonus; ?></span></a></br>
                        <a href="#">
                            Wisdom 
                            <span class="badge"><?php echo $Character->Wisdom; ?></span>
                            <span class="badge"><?php echo $Character->WisdomBonus; ?></span>
                        </a></br>
                        <a href="#">Charisma 
                            <span class="badge"><?php echo $Character->Charisma; ?></span>
                            <span class="badge"><?php echo $Character->CharismaBonus; ?></span>
                        </a></br>

                    </li>
                </ul>  
                </div>
            </div>
        </div>
    </div>
</div>
<?php
}

UpdateCharacterStat.php

<?php
$Stat = filter_input(INPUT_POST, 'Stat');

include "../../../inc/dbconnect.php";

$sql = "UPDATE TBL_Character SET Strength= Strength + 1 WHERE ID=2";
echo "Strength increase selected!";
if ($connection->query($sql) === TRUE) {

} else {
    echo "Error updating record: " . $connection->error;
}

您应该首先创建一个名为 getCharacterStats.php 的页面。然后使用 javascripts XMLHTTPREQUEST 在更新后加载数据。