简体   繁体   中英

redirect to another html file using php

I currently want to redirect to another HTML file (ie dashboard.html) after the user has successfully. I know I can use header to solve this, but I am not sure where should I add it into my code.

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));
    }

Cheers for your kindly help.

You can either add an <a> tag into the code built by PHP:

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

Or, you can use javascript/jQuery to trap a user click and redirect them to a new page:

<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>

Or, if headers have already been sent and the PHP header() method specified in joakkinen's answer won't work, you can echo this HTML:

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

(the content=0 represent number of seconds delay before redirect)

I can add ...

header('Location: dashboard.html');
exit;
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!'); 
    }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM