PHP page redirect
Yes, you would use the header function.
/* Redirect browser */
header("Location: http://www.yourwebsite.com/user.php");
exit();
It is a good practice to call exit()
right after it so that code below it does not get executed.
Also, from the documentation:
Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP. It is a very common error to read code with include(), or require(), functions, or another file access function, and have spaces or empty lines that are output before header() is called. The same problem exists when using a single PHP/HTML file.
This means you should not echo anything right before the header()
function, as doing so will more than likely throw an error. Also, you will need to verify that this code gets run before any other output as well.
How to redirect to another page using PHP
That's the problem. I've outputted a bunch of information (including the HTML to build the login page itself). So how do I redirect the user from one page to the next?
This means your application design is pretty broken. You shouldn't be doing output while your business logic is running. Go an use a template engine (like Smarty) or quickfix it by using output buffering).
Another option (not a good one though!) would be outputting JavaScript to redirect:
<script type="text/javascript">location.href = 'newurl';</script>
PHP multiple redirects
It's fine except there is no need to have two location calls, and you don't need the else:
$url = "index.php";
if ((isset($_POST['listing_type'])) && (!empty($_POST['listing_type']))) {
// Switch statement
}
header("Location: $url");
Make dynamic redirection php based on parameters sent
Even though this question has an accepted answer, I am providing the following solution because:
- The question was tagged as PHP, not JavaScript
- The accepted answer is not a dynamic solution as was originally requested
then redirect to a dynamic url based on the parameters received.
Example URL: http://localhost/test.php?hello=world&foo=bar
Getting the query string parameters:
$query = parse_url($_SERVER['REQUEST_URI'], PHP_URL_QUERY);
parse_str($query, $result);
echo '<pre>';
print_r($result);
echo '</pre>';
Outputs:
Array
(
[hello] => world
[foo] => bar
)
To perform the redirect:
header('Location: http://www.example.com/?' . http_build_query($result));
This would redirect to: http://www.example.com/?hello=world&foo=bar
Is it possible to redirect using PHP?
You could do something like this
header("Location: http://example.com/tryagain.php");
die();
How to redirect a page using onclick event in php?
You can't use php code client-side. You need to use javascript.
<input type="button" value="Home" class="homebutton" id="btnHome"
onClick="document.location.href='some/page'" />
However, you really shouldn't be using inline js (like onclick here). Study about this here: https://www.google.com/search?q=Why+is+inline+js+bad%3F
Here's a clean way of doing this: Live demo (click).
Markup:
<button id="myBtn">Redirect</button>
JavaScript:
var btn = document.getElementById('myBtn');
btn.addEventListener('click', function() {
document.location.href = 'some/page';
});
If you need to write in the location with php:
<button id="myBtn">Redirect</button>
<script>
var btn = document.getElementById('myBtn');
btn.addEventListener('click', function() {
document.location.href = '<?php echo $page; ?>';
});
</script>
Related Topics
How to Add Dynamic Filter Using PHP MySQL Ajax
Get Selected Option in PHP Without Pressing Submit
How to Update Field to Add Value to Existing Value
Laravel 5.5 Error 500 in Cpanel Shared Hosting
Calculate Difference Between 2 Times in Hours in PHP
How to Replace Newline or \R\N With <Br/>
Php Fatal Error: Uncaught Pdoexception: Could Not Find Driver
How to Set Current Date as Default Value of an Input
I Have Three Columns, I Want to Combine Them All in One Main_Table
How to Pass Checkbox Value 0 If Not Checked and 1 If Checked Using Array Laravel
Dompdf Table Fixed Column Width and Break Long Text
Checking Dates If Between Range of Dates in Laravel 5
How to Use Return Inside a Recursive Function in PHP
Send Attachments With PHP Mail()
The Get Method Is Not Supported for This Route. Supported Methods: Put. But I'M Using Put
Dynamically Display a CSV File as an HTML Table on a Web Page