使用php重定向到另一个html文件


redirect to another html file using php

我目前想在用户成功后重定向到另一个HTML文件(即dashboard.HTML)。我知道我可以使用头来解决这个问题,但我不确定我应该在哪里将它添加到代码中。

if (mysqli_query($Link, $Query)) {
        $lastID = mysqli_insert_id($Link);
        $Query2 = "INSERT INTO $table_2 VALUES (NULL,
        '".$lastID."')";
        if (mysqli_query($Link, $Query2)) {
            $message = "You've sucessfully created the account!";
            echo json_encode(array('success'=>'true', 'action'=>'login','html'=>$message, 'console.log'=>$Query));
        }
        else {
            $message = "Error occur in query2";
            echo json_encode(array('action'=>'error','html'=>$message, 'console.log'=>$Query));
        }
    }
    else {
        $message = "Error in query1";
        echo json_encode(array('action'=>'error','html'=>$message, 'console.log'=>$Query));
    }

为你的善意帮助干杯。

if (mysqli_query($Link, $Query)) {
        $lastID = mysqli_insert_id($Link);
        $Query2 = "INSERT INTO $table_2 VALUES (NULL,
        '".$lastID."')";
        if (mysqli_query($Link, $Query2)) {
            $message = "You've sucessfully created the account!";
            echo json_encode(array('success'=>'true', 'action'=>'login','html'=>$message, 'console.log'=>$Query));
        }
        else {
            $message = "Error occur in query2";
            echo json_encode(array('action'=>'error','html'=>$message, 'console.log'=>$Query));
        }
    }
    else {
        $message = "Error in query1";
        echo json_encode(array('action'=>'error','html'=>$message, 'console.log'=>$Query));
    }

jQuery

$.ajax( { 
    type: 'POST', 
    dataType: 'json', 
    data: postData, 
    url: 'n3228691.scm.tees.ac.uk/Yii/MACC/models/…';, 
    success: function(data) { 
        console.log(data); 
        if(data.action === "login"){
            window.location="dashboard.html"; //succeed insert
        }else{
            alert('There was an error handling your registration!'); 
        }
    }, 
    error: function(data) { 
        alert('There was an error handling your registration!'); 
    }
});

我可以添加。。。

header('Location: dashboard.html');
exit;

您可以在PHP构建的代码中添加<a>标记:

<a href="new_page.php">Click this link for new page</a>

或者,您可以使用javascript/jQuery捕获用户点击并将其重定向到新页面:

<div id="redir">Click this link</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script>
    $(function(){
        $('#redir').click(function(){
            window.location.href = 'new_page.php'
        });
    });
</script>

或者,如果已经发送了头,而joakkinen的答案中指定的PHP header()方法不起作用,则可以回显以下HTML:

echo '<meta HTTP-EQUIV="REFRESH" content="0; url=new_page.php">'; 

(content=0表示重定向前的延迟秒数)