如何显示带有超时的警报框,然后使用php自动重定向到另一个页面

如何显示带有超时的警报框,然后使用php自动重定向到另一个页面,php,Php,嗨,当数据库表中已有特定数据时,我必须设置一个带有超时的消息框。然后重定向到另一个页面。我的问题是,当页面被重定向时,没有显示警报框 控制语句php: $count=0; $sql="SELECT main_sec_name from main_sec_temp"; if($r=mysqli_query($con,$sql)) { while($result= $r->fetch_row()) { $count=$count+1; } }

嗨,当数据库表中已有特定数据时,我必须设置一个带有超时的消息框。然后重定向到另一个页面。我的问题是,当页面被重定向时,没有显示警报框

控制语句php:

$count=0;
$sql="SELECT main_sec_name from main_sec_temp";
if($r=mysqli_query($con,$sql)) {
    while($result= $r->fetch_row()) {
        $count=$count+1;        
    }
}   
if($count==0) { 
    if(mysqli_query($con,"INSERT INTO main_sec_temp(main_section_order,main_sec_name,main_number_of_ques,attempts) VALUES('','$sec_name','$no_question','$no_attempts')")) { 
        echo "Successfully Inserted";          
        header("location:index.php");       
    }else{        
        echo "Insertion Failed";        
    }
} else { //When Data is already exist 
    echo "<script>alert('already exist')</script>";
    header("location:index.php");   
}
$count=0;
$sql=“从main_sec_temp中选择main_sec_名称”;
if($r=mysqli_查询($con,$sql)){
而($result=$r->fetch_row()){
$count=$count+1;
}
}   
如果($count==0){
如果(mysqli_query($con,“插入到main_section_temp(main_section_order,main_section_name,main_number_of_ques,attempts))值(“'$sec_name','$no_Questions','$no_attempts')){
echo“成功插入”;
标题(“location:index.php”);
}否则{
回显“插入失败”;
}
}else{//当数据已经存在时
回显“警报(‘已存在’)”;
标题(“location:index.php”);
}

您试图在示例中混合使用服务器端(PHP)和客户端(Javascript)代码。使用
header('Location:…')
将由PHP执行,然后用户才有机会看到页面的内容(在页面上显示任何内容之前也需要使用页眉)

另外,使用javascript alert()需要通过按OK按钮进行用户交互,因此不能使用timeout重定向用户

更好的方法是这样的:

/// display html message and   the javascript part of your code
echo "<h3 class='error'>Already exist</h3>";
echo "<script>";
echo "setTimeout(function(){ ";
echo "   document.location='index.php';";
echo "}, 3000);";  // redirect after 3 seconds
echo "</script>";         
///显示html消息和代码的javascript部分
回声“已经存在”;
回声“;
echo“setTimeout(function(){”;
echo“document.location='index.php';”;
回声“},3000);”;//3秒后重定向
回声“;

您试图在示例中混合使用服务器端(PHP)和客户端(Javascript)代码。使用
header('Location:…')
将由PHP执行,然后用户才有机会看到页面的内容(在页面上显示任何内容之前也需要使用页眉)

另外,使用javascript alert()需要通过按OK按钮进行用户交互,因此不能使用timeout重定向用户

更好的方法是这样的:

/// display html message and   the javascript part of your code
echo "<h3 class='error'>Already exist</h3>";
echo "<script>";
echo "setTimeout(function(){ ";
echo "   document.location='index.php';";
echo "}, 3000);";  // redirect after 3 seconds
echo "</script>";         
///显示html消息和代码的javascript部分
回声“已经存在”;
回声“;
echo“setTimeout(function(){”;
echo“document.location='index.php';”;
回声“},3000);”;//3秒后重定向
回声“;

工作正常,先生,谢谢不客气,很高兴为您提供帮助工作正常,先生,谢谢不客气,很高兴为您提供帮助