如果有标题,如何在PHP中使用javascript?

时间:2013-11-05 06:12:06

标签: javascript php

if(($row["status"])!="valid")
{
    $stats=$row["status"];
    echo "<script type='text/javascript'>";
    echo "alert('Your Status is $stats..Please Try Again Later...');";
    echo "</script>";
    header ("location: index.php");
}

它是php代码的一部分。如果我在标题之前使用//,javascript run ..但是当我不使用//时,它直接转到index.php。解决办法是什么??或者是否有任何方法可以调用javascript函数来显示来自此if条件的消息?

4 个答案:

答案 0 :(得分:0)

在此方案中,将标题替换为 document.location.href 因此您可以显示警告框,当您单击“确定”时,它会重定向到您的index.php页面

if(($row["status"])!="valid")
{
    $stats=$row["status"];
    echo "<script type='text/javascript'>";
    echo "alert('Your Status is $stats..Please Try Again Later...');";
    echo "</script>";
    echo "<script>document.location.href='index.php'</script>";
}

答案 1 :(得分:0)

你能试试吗,

        <?php 
            if(isset($row["status"]) && trim($row["status"])!="valid")
            {
                    $stats=$row["status"];
                    echo "<script type='text/javascript'>";
                    echo "alert('Your Status is $stats..Please Try Again Later...');";
                    echo "window.location.href='index.php';"; // instead php header you can use javascript location.hrfe 
                    echo "</script>";                           
            }

        ?>

答案 2 :(得分:0)

try

if(($row["status"])!="valid")
{
$stats=$row["status"];
echo "<script type='text/javascript'>";
echo "alert('Your Status is $stats..Please Try Again Later...');";
echo "</script>";
}
else{
header ("location: index.php");
}

答案 3 :(得分:0)

您也可以尝试这样

if(($row["status"])!="valid")
{
$stats=$row["status"];
echo "<script type='text/javascript'>";
echo "alert('Your Status is $stats..Please Try Again Later...');";
echo "location: index.php";
echo "</script>";
}