i am trying to redirect after submitting some data into my database to example.php i have the form and it submits perfect but cant get the redirect to work i have tried a few methods i have seen on the internet but nothing worked
<html>
<head>
<title>MySQLi Create Record</title>
</head>
<body>
<?php
$action = isset($_POST['action']) ? $_POST['action'] : "";
if($action=='create'){
//include database connection
include 'db_connect.php';
//write query
$query = "insert into referb
set
make = '".$mysqli->real_escape_string($_POST['make'])."',
model = '".$mysqli->real_escape_string($_POST['model'])."',
unitt = '".$mysqli->real_escape_string($_POST['unitt'])."'";
if( $mysqli->query($query) ) {
echo " header( 'Location: example.php' ) ;";
}else{
echo "Database Error: Unable to create record.";
}
$mysqli->close();
}
?>
<!--we have our html form here where user information will be entered-->
<form action='#' method='post' border='0'>
<table>
<tr>
<td>Manufactor</td>
<td><input type='text' name='make' /></td>
</tr>
<tr>
<td>Model</td>
<td><input type='text' name='model' /></td>
</tr>
<tr>
<td>Unit Type</td>
<td>
<select name='unitt'>
<option>Laptop</option>
<option>Computer</option>
<option>Tablet</option>
</select></td>
</tr>
<td></td>
<td>
<input type='hidden' name='action' value='create' />
<input type='submit' value='Save' />
<a href='index.php'>Back to index</a>
</td>
</tr>
</table>
</form>
</body>
</html>
the call to header() should not be echo'd. that line should be changed to:
header('Location: example.php');
exit();
calling exit() afterards will then stop further execution of the script.
Do not echo header("Location: example.php)".
It is a php function so you just write
header("Location:example.php");
You can however setup an if
directive.
You have echo " header( 'Location: example.php' ) ;";
it would need to read as header('Location: file.php');
only.
The correct method of doing a header redirect is:
header('Location: example.php');
if( $mysqli->query($query) ) {
header('Location: example.php');
}else{
echo "Database Error: Unable to create record.";
}
$mysqli->close();
}
Visit the PHP.net on the header()
function: http://php.net/manual/en/function.header.php
Step 1: First execute your query to insert data into database
Step 2: Close database connection
Step 3: Just put>>> header('Location: example.php');
this a php script that redirect the user to another URL put this after the right condition or after instructions of your code
<html>
<?php
header('Location: http://www.example.com/');
exit;
?>
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.