在Ajax操作后刷新模式

时间:2015-05-13 14:26:37

标签: php jquery ajax twitter-bootstrap bootstrap-modal

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

在ViewCharacter.php页面中,您将看到一个包含最新数据的页面,然后您可以单击铅笔按钮“#ButtonEditCharacterStats”来编辑统计数据,然后它将为您打开一个模态最新数据。单击“加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;
}

1 个答案:

答案 0 :(得分:1)

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