即使条件为假,为什么AJAX也输出最后一个echo语句?的PHP

时间:2019-03-08 20:59:39

标签: php ajax

除了我的用户看到“您必须在犯罪之间等待60秒!”之外,我无法弄清为什么POST请求会通过,PHP代码开始得到评估,一切都按预期进行。即使不应显示为与else相对应的if语句为真,那么为什么还要显示此输出呢?它应该进一步显示成功消息,但是即使所有功能都正常执行,也不能显示成功消息。

AJAX请求:

$(document).ready(function() {
    $('#purse').click(function() {
        $.ajax(
        {   
            type : "POST",
            url: "crimes-petty.php",
            data: {purse: 'true'},
            dataType: 'json',
            success: function(data) {
                $('div#outputContainer').html(JSON.parse(data));
            }

        });
    });
});

PHP代码:

if(isset($_POST['purse'])) {
    if($char_nextcrime <= time()) {
        if($char_gexp >= 400) {
            $crime_chance = 1;
        } else {
            if($luckCategory == 1) {
                $crime_chance = rand(1,9);
            } elseif($luckCategory == 2) {
                $crime_chance = rand(1,16);
            } elseif($luckCategory == 3) {
                $crime_chance = rand(1,25);
            }
        }

        $crime_gexp = 2 * $gexp_multiplier;
        $crime_money = rand(20,100);
        $crime_jailchance = rand(1, 30);

        if($luckCategory == 1) {
            $itemdrop_chance = rand(1,75);
        } elseif($luckCategory == 2) {
            $itemdrop_chance = rand(1,50);
        } elseif($luckCategory == 3) {
            $itemdrop_chance = rand(1,35);
        }

        if($crime_chance == 2 or $crime_chance == 6 or $crime_chance == 7 or $crime_chance == 4) {
            echo("You have failed to snatch any purses today!");
            addCrimeTime($db, $char_id, 60);
            if($crime_jailchance == 23) {
                echo("\nThe cops busted you for this one!");
                $char_jailtimestarted = time();
                addJailTime($db, $char_id, time() + $crime_jailchance, $char_jailtimestarted);
            }
        } else {
            echo("<div class='alert alert-success'>You have successfully ran off with " . $crime_money . " dollars after a hard day of snagging purses!</div>");
            successfulCrime($db, $char_id, $char_money, $crime_money, $totalCrimes, $char_gexp, $crime_gexp);
            increaseStatsForCrimes($db, $char_id, "Petty", $luckCategory);
            calculatePower($db, $char_id);
            addCrimeTime($db, $char_id, 60);
            if($itemdrop_chance == 4 or $itemdrop_chance == 17 or $itemdrop_chance == 10 or $itemdrop_chance == 2 or $itemdrop_chance == 6) {
                $func_execute = findItemName($db, $purse_loottable[0]);
                $event_text = "You have picked up a $func_execute!";

                echo("<br>You have picked up a " . $func_execute . "!");

                event_add($char_id, $event_text);
                giveItem($db, $char_id, $purse_loottable[0], 1);
            }
        }
    } else {
        echo("<div class='alert alert-danger'>You must wait 60 seconds between crimes!</div>");
    }
}

HTML:

<div id="outputContainer"></div>
<button type="button" name="purse" id="purse" class="btn btn-dark center-block">Submit</button>

所有代码:

<body>
    <div class="container p-0" style="background-color: white; border-style: solid">

        <div class="my-auto">
        <h1 class="mb-0 text-center">Petty Crimes</h1>
        <h3 class="mb-0 text-center">Not the best, but it pays the bills</h3>
        <div class="text-center">
        <br>
        <script>

            $(document).ready(function() {
                $('#purse').click(function() {
                    $.ajax(
                    {   
                        type : "POST",
                        url: "crimes-petty.php",
                        data: {purse: 'true'},
                        dataType: 'html',
                        success: function(data) {
                            $('div#outputContainer').html(data);
                        }

                    });
                });
            });

        </script>
        <?php
                if(isset($_POST['purse'])) {
                        if($char_nextcrime <= time()) {
                            if($char_gexp >= 400) {
                                $crime_chance = 1;
                            } else {
                                if($luckCategory == 1) {
                                    $crime_chance = rand(1,9);
                                } elseif($luckCategory == 2) {
                                    $crime_chance = rand(1,16);
                                } elseif($luckCategory == 3) {
                                    $crime_chance = rand(1,25);
                                }
                            }

                            $crime_gexp = 2 * $gexp_multiplier;
                            $crime_money = rand(20,100);
                            $crime_jailchance = rand(1, 30);

                            if($luckCategory == 1) {
                                $itemdrop_chance = rand(1,75);
                            } elseif($luckCategory == 2) {
                                $itemdrop_chance = rand(1,50);
                            } elseif($luckCategory == 3) {
                                $itemdrop_chance = rand(1,35);
                            }

                            if($crime_chance == 2 or $crime_chance == 6 or $crime_chance == 7 or $crime_chance == 4) {
                                echo("You have failed to snatch any purses today!");
                                addCrimeTime($db, $char_id, 60);
                                if($crime_jailchance == 23) {
                                    echo("\nThe cops busted you for this one!");
                                    $char_jailtimestarted = time();
                                    addJailTime($db, $char_id, time() + $crime_jailchance, $char_jailtimestarted);
                                }
                            } else {
                                echo("<div class='alert alert-success'>You have successfully ran off with " . $crime_money . " dollars after a hard day of snagging purses!</div>");
                                successfulCrime($db, $char_id, $char_money, $crime_money, $totalCrimes, $char_gexp, $crime_gexp);
                                increaseStatsForCrimes($db, $char_id, "Petty", $luckCategory);
                                calculatePower($db, $char_id);
                                addCrimeTime($db, $char_id, 60);
                                if($itemdrop_chance == 4 or $itemdrop_chance == 17 or $itemdrop_chance == 10 or $itemdrop_chance == 2 or $itemdrop_chance == 6) {
                                    $func_execute = findItemName($db, $purse_loottable[0]);
                                    $event_text = "You have picked up a $func_execute!";

                                    echo("<br>You have picked up a " . $func_execute . "!");

                                    event_add($char_id, $event_text);
                                    giveItem($db, $char_id, $purse_loottable[0], 1);
                                }
                            }
                        } else {
                            echo("<div class='alert alert-danger'>You must wait 60 seconds between crimes!</div>");
                        }
                    exit();
                 }
                 if(isset($_POST["checks"])) {
                    if($char_nextcrime <= time()) {
                        if($char_gexp >= 800) {
                            $crime_chance = 1;
                        } else {
                            if($luckCategory == 1) {
                                $crime_chance = rand(1,9);
                            } elseif($luckCategory == 2) {
                                $crime_chance = rand(1,16);
                            } elseif($luckCategory == 3) {
                                $crime_chance = rand(1,25);
                            }
                        }

                        $crime_gexp = 4 * $gexp_multiplier;
                        $crime_money = rand(50,250);

                        if($crime_chance == 2 or $crime_chance == 6 or $crime_chance == 7 or $crime_chance == 4) {
                            echo("You have failed to forge checks today!");
                            addCrimeTime($db, $char_id, 60);
                        } else {
                            echo("You have successfully forged a few checks, they are worth about " . $crime_money . " dollars!");
                            successfulCrime($db, $char_id, $char_money, $crime_money, $totalCrimes, $char_gexp, $crime_gexp);
                            increaseStatsForCrimes($db, $char_id, "Petty", $luckCategory);
                            calculatePower($db, $char_id);
                            addCrimeTime($db, $char_id, 60);
                        }
                    } else {
                        echo("<div class='alert alert-danger'>You must wait 60 seconds between crimes!</div>");
                    }
                    exit();
                } 
                if(isset($_POST["cars"])) {
                    if($char_nextcrime <= time()) {
                        if($char_gexp >= 1000) {
                            $crime_chance = 1;
                        } else {
                            if($luckCategory == 1) {
                                $crime_chance = rand(1,9);
                            } elseif($luckCategory == 2) {
                                $crime_chance = rand(1,16);
                            } elseif($luckCategory == 3) {
                                $crime_chance = rand(1,25);
                            }
                        }

                        $crime_gexp = 5 * $gexp_multiplier;
                        $crime_money = rand(120,300);

                        if($crime_chance == 2 or $crime_chance == 6 or $crime_chance == 7 or $crime_chance == 4) {
                            echo("You have failed to sell any stolen cars!");
                            addCrimeTime($db, $char_id, 60);
                        } else {
                            successfulCrime($db, $char_id, $char_money, $crime_money, $totalCrimes, $char_gexp, $crime_gexp);
                            increaseStatsForCrimes($db, $char_id, "Petty", $luckCategory);
                            calculatePower($db, $char_id);
                            addCrimeTime($db, $char_id, 60);
                            echo("<div class='alert alert-success'>You have successfully sold a few cars to the local chop shop for about " . $crime_money . " dollars!</div>");
                        }
                    } else {
                        echo("<div class='alert alert-danger'>You must wait 60 seconds between crimes!</div>");
                    }
                    exit();
                }

        ?>
            <div id="outputContainer"></div>
            <div class="subheading mb-5 text-center"><br>Steal a few purses.
            <button type="submit" name="purse" id="purse" class="btn btn-dark center-block">Submit</button>
            <?php if($char_gexp >= 200) : ?>
                <div class="subheading mb-5 text-center"><br>Forge checks.
                <button type="submit" name="checks" id="checks" class="btn btn-dark center-block">Submit</button>
            <?php endif ?>
            <?php if($char_gexp >= 400) : ?>
                <div class="subheading mb-5 text-center"><br>Steal cars.
                <button type="submit" name="cars" id="cars" class="btn btn-dark center-block">Submit</button>
            <?php endif ?>
                </div>
                </div>
                </div>
            </div>
        </div>
    </div>

1 个答案:

答案 0 :(得分:1)

您需要将所有处理AJAX请求的代码移到开头,以便在执行此操作之前不会打印任何其他HTML。

将它分成两个单独的脚本会更容易,一个显示常规页面,另一个处理AJAX请求。

<?php
if(isset($_POST['purse'])) {
    if($char_nextcrime <= time()) {
        if($char_gexp >= 400) {
            $crime_chance = 1;
        } else {
            if($luckCategory == 1) {
                $crime_chance = rand(1,9);
            } elseif($luckCategory == 2) {
                $crime_chance = rand(1,16);
            } elseif($luckCategory == 3) {
                $crime_chance = rand(1,25);
            }
        }

        $crime_gexp = 2 * $gexp_multiplier;
        $crime_money = rand(20,100);
        $crime_jailchance = rand(1, 30);

        if($luckCategory == 1) {
            $itemdrop_chance = rand(1,75);
        } elseif($luckCategory == 2) {
            $itemdrop_chance = rand(1,50);
        } elseif($luckCategory == 3) {
            $itemdrop_chance = rand(1,35);
        }

        if($crime_chance == 2 or $crime_chance == 6 or $crime_chance == 7 or $crime_chance == 4) {
            echo("You have failed to snatch any purses today!");
            addCrimeTime($db, $char_id, 60);
            if($crime_jailchance == 23) {
                echo("\nThe cops busted you for this one!");
                $char_jailtimestarted = time();
                addJailTime($db, $char_id, time() + $crime_jailchance, $char_jailtimestarted);
            }
        } else {
            echo("<div class='alert alert-success'>You have successfully ran off with " . $crime_money . " dollars after a hard day of snagging purses!</div>");
            successfulCrime($db, $char_id, $char_money, $crime_money, $totalCrimes, $char_gexp, $crime_gexp);
            increaseStatsForCrimes($db, $char_id, "Petty", $luckCategory);
            calculatePower($db, $char_id);
            addCrimeTime($db, $char_id, 60);
            if($itemdrop_chance == 4 or $itemdrop_chance == 17 or $itemdrop_chance == 10 or $itemdrop_chance == 2 or $itemdrop_chance == 6) {
                $func_execute = findItemName($db, $purse_loottable[0]);
                $event_text = "You have picked up a $func_execute!";

                echo("<br>You have picked up a " . $func_execute . "!");

                event_add($char_id, $event_text);
                giveItem($db, $char_id, $purse_loottable[0], 1);
            }
        }
    } else {
        echo("<div class='alert alert-danger'>You must wait 60 seconds between crimes!</div>");
    }
    exit();
}
if(isset($_POST["checks"])) {
    if($char_nextcrime <= time()) {
        if($char_gexp >= 800) {
            $crime_chance = 1;
        } else {
            if($luckCategory == 1) {
                $crime_chance = rand(1,9);
            } elseif($luckCategory == 2) {
                $crime_chance = rand(1,16);
            } elseif($luckCategory == 3) {
                $crime_chance = rand(1,25);
            }
        }

        $crime_gexp = 4 * $gexp_multiplier;
        $crime_money = rand(50,250);

        if($crime_chance == 2 or $crime_chance == 6 or $crime_chance == 7 or $crime_chance == 4) {
            echo("You have failed to forge checks today!");
            addCrimeTime($db, $char_id, 60);
        } else {
            echo("You have successfully forged a few checks, they are worth about " . $crime_money . " dollars!");
            successfulCrime($db, $char_id, $char_money, $crime_money, $totalCrimes, $char_gexp, $crime_gexp);
            increaseStatsForCrimes($db, $char_id, "Petty", $luckCategory);
            calculatePower($db, $char_id);
            addCrimeTime($db, $char_id, 60);
        }
    } else {
        echo("<div class='alert alert-danger'>You must wait 60 seconds between crimes!</div>");
    }
    exit();
} 
if(isset($_POST["cars"])) {
    if($char_nextcrime <= time()) {
        if($char_gexp >= 1000) {
            $crime_chance = 1;
        } else {
            if($luckCategory == 1) {
                $crime_chance = rand(1,9);
            } elseif($luckCategory == 2) {
                $crime_chance = rand(1,16);
            } elseif($luckCategory == 3) {
                $crime_chance = rand(1,25);
            }
        }

        $crime_gexp = 5 * $gexp_multiplier;
        $crime_money = rand(120,300);

        if($crime_chance == 2 or $crime_chance == 6 or $crime_chance == 7 or $crime_chance == 4) {
            echo("You have failed to sell any stolen cars!");
            addCrimeTime($db, $char_id, 60);
        } else {
            successfulCrime($db, $char_id, $char_money, $crime_money, $totalCrimes, $char_gexp, $crime_gexp);
            increaseStatsForCrimes($db, $char_id, "Petty", $luckCategory);
            calculatePower($db, $char_id);
            addCrimeTime($db, $char_id, 60);
            echo("<div class='alert alert-success'>You have successfully sold a few cars to the local chop shop for about " . $crime_money . " dollars!</div>");
        }
    } else {
        echo("<div class='alert alert-danger'>You must wait 60 seconds between crimes!</div>");
    }
    exit();
}

?>

<body>
<div class="container p-0" style="background-color: white; border-style: solid">

    <div class="my-auto">
    <h1 class="mb-0 text-center">Petty Crimes</h1>
    <h3 class="mb-0 text-center">Not the best, but it pays the bills</h3>
    <div class="text-center">
    <br>
    <script>

    $(document).ready(function() {
            $('#purse').click(function() {
                    $.ajax(
                        {   
                        type : "POST",
                                url: "crimes-petty.php",
                                data: {purse: 'true'},
                                dataType: 'html',
                                success: function(data) {
                                $('div#outputContainer').html(data);
                            }

                        });
                });
        });

</script>
<div id="outputContainer"></div>
    <div class="subheading mb-5 text-center"><br>Steal a few purses.
    <button type="submit" name="purse" id="purse" class="btn btn-dark center-block">Submit</button>
    <?php if($char_gexp >= 200) : ?>
    <div class="subheading mb-5 text-center"><br>Forge checks.
    <button type="submit" name="checks" id="checks" class="btn btn-dark center-block">Submit</button>
    <?php endif ?>
    <?php if($char_gexp >= 400) : ?>
    <div class="subheading mb-5 text-center"><br>Steal cars.
    <button type="submit" name="cars" id="cars" class="btn btn-dark center-block">Submit</button>
    <?php endif ?>
    </div>
    </div>
    </div>
    </div>
    </div>
    </div>