无法回复来自php的javascript警报

时间:2016-09-29 01:29:34

标签: javascript php

我已经找到了答案,但我知道如何从php获取警报我只是不知道我在这段特定代码上做错了什么。
我有这个工作,直到我添加了if语句。

if ($errors) {
        echo "<script type='text/javascript'>";
        echo "alert('Records Were Uploaded');";
        echo "window.location.href = 'EmployeePicker.php';";
        echo "</script>";
    } else {
        echo "<script type='text/javascript'>";
        echo "alert('There was a problem with your file');";
        echo "window.location.href = 'csvUpload.php';";
        echo "</script>";
    }

当它刚好时它工作得很好。

        echo "<script type='text/javascript'>";
        echo "alert('Records Were Uploaded');";
        echo "window.location.href = 'EmployeePicker.php';";
        echo "</script>";

如果对所有内容进行评论并且只是做

        echo "<script type='text/javascript'>";
        echo "alert('There was a problem with your file');";
        echo "window.location.href = 'csvUpload.php';";
        echo "</script>";

这不起作用。我很困惑。

指出第二个警报不起作用是没有意义的。

我忘了提及,在上面的if语句中,第一个警报会起作用,它是我无法触发的第二个警告。

对于这种混淆感到抱歉,$ error是一个bool。如果确实上传了一个filw,如果是false则不是。

2 个答案:

答案 0 :(得分:0)

我认为你正在做你想要的事情: 您的if情况与内部执行的操作不匹配。 也许你应该这样做:

if (!$errors) {
        echo "<script type='text/javascript'>";
        echo "alert('Records Were Uploaded');";
        echo "window.location.href = 'EmployeePicker.php';";
        echo "</script>";
    } else {
        echo "<script type='text/javascript'>";
        echo "alert('There was a problem with your file');";
        echo "window.location.href = 'csvUpload.php';";
        echo "</script>";
    }

你也可以节省echo字 - 并使其更具可读性 - 做某事:

echo <<<HTML
<script type='text/javascript'>
    alert('Records Were Uploaded');
    window.location.href = 'EmployeePicker.php';
</script>
HTML;

如果你想要:)

[编辑]请注意,根据$errors保持(整数或数组),您可以检查!$errors是否为整数或!count($errors)如果是数组的错误。

[编辑]尝试将您的第二段代码作为独立代码

如果你需要追踪错误,你需要一点一点地隔离! 首先尝试code 1作为一个新的php文件,如果它没有警告并重定向你的系统,那么你的配置出了问题。 如果它有效,你的代码逻辑就会出错。 我也可以在没有pb的情况下运行code 2,首先将$errors var设置为1或0。

代码1

<?php
$errors = 1;

    echo "<script type='text/javascript'>";
    echo "alert('There was a problem with your file');";
    echo "window.location.href = 'csvUpload.php';";
    echo "</script>";

?>

代码2

<?php
$errors = 1;
if ($errors) {
        echo "<script type='text/javascript'>";
        echo "alert('Records Were Uploaded');";
        // echo "window.location.href = 'EmployeePicker.php';";
        echo "</script>";
    } else {
        echo "<script type='text/javascript'>";
        echo "alert('There was a problem with your file');";
        // echo "window.location.href = 'csvUpload.php';";
        echo "</script>";
    }
?>

关于你的$errors var,如果true表示没有错误,你肯定应该将它重命名为$uploadSuccessful不那么棘手。 ;)

答案 1 :(得分:0)

应该好好回应。尝试检查您的php错误日志文件,看看是否有任何关于该问题的线索。

可能对你有帮助的一个最可能的个人喜好就是将逻辑分开一点。请参阅下文。

<?php

// STATUS
$errors = true;

// OUTPUTS
$upload_success = "<script type='text/javascript'>";
    $upload_success .= "alert('Records Were Uploaded');";
    $upload_success .= "window.location.href = 'EmployeePicker.php';";
$upload_success .= "</script>";

$upload_fail = "<script type='text/javascript'>";
    $upload_fail .= "alert('There was a problem with your file');";
    $upload_fail .= "window.location.href = 'csvUpload.php';";
$upload_fail .= "</script>";

//LOGIC
if ($errors) {
    echo $upload_success;
} else {
    echo $upload_fail;
}

?>