Reputation: 215
I am building an application where details are displayed (admin.php).The user clicks on edit(edit.php) and he is redirected to another page to edit his details. Once the edit is done the values are updated and I want to redirect to the previous page once again where the details are displayed. All the operations carry out as desired , but when I try to redirect inside the "submit" to the previous page it doesn't occur.
Admin.php
$event_id = $_SESSION['fedit'];
if($_GET['edit'])
{
$url = "edit.php?event_id=". $event_id;
header("Location: $url");
}
Edit.php
if($_GET["Submit"])
{
$val=$_GET['event'];
$remail=$_GET['email'];
$rname = $_GET['ename'];
$sqt=mysql_query("UPDATE users SET NAME='$rname',EMAIL='$remail' WHERE EID ='$val'");
$page= "admin.php";
header("Location: $page");
}
Upvotes: 1
Views: 20448
Reputation: 309
I don't know where you have placed this code on the page but if somehow a http header output is being sent before that header(location:"") then it wont work. Try the suggestions by other people, but if all else fails, you might wanna use JS.
echo "<script>window.location.assign("admin.php")</script>";
Use that instead of
header("Location: $page");
and it should work I guess.
Upvotes: 3
Reputation: 157839
It's very hard to tell what are you doing from the incomplete code you posted.
However, it looks like that not redirect but form submission is broken.
There are also 2 serious flaws in your code:
Anyway, here is an example of such an application (though editing only one field. you can easily add more) which works.
<?
mysql_connect();
mysql_select_db("new");
$table = "test";
if($_SERVER['REQUEST_METHOD']=='POST') { //form handler part:
$name = mysql_real_escape_string($_POST['name']);
if ($id = intval($_POST['id'])) {
$query="UPDATE $table SET name='$name' WHERE id=$id";
} else {
$query="INSERT INTO $table SET name='$name'";
}
mysql_query($query) or trigger_error(mysql_error()." in ".$query);
header("Location: http://".$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']);
exit;
}
if (!isset($_GET['id'])) { //listing part:
$LIST=array();
$query="SELECT * FROM $table";
$res=mysql_query($query);
while($row=mysql_fetch_assoc($res)) $LIST[]=$row;
include 'list.php';
} else { // form displaying part:
if ($id=intval($_GET['id'])) {
$query="SELECT * FROM $table WHERE id=$id";
$res=mysql_query($query);
$row=mysql_fetch_assoc($res);
foreach ($row as $k => $v) $row[$k]=htmlspecialchars($v);
} else {
$row['name']='';
$row['id']=0;
}
include 'form.php';
}
?>
And you will need 2 templates too, to hold your HTML
form.php
<form method="POST">
<input type="text" name="name" value="<?=$row['name']?>"><br>
<input type="hidden" name="id" value="<?=$row['id']?>">
<input type="submit"><br>
<a href="?">Return to the list</a>
</form>
list.php
<a href="?id=0">Add item</a>
<? foreach ($LIST as $row): ?>
<li><a href="?id=<?=$row['id']?>"><?=$row['name']?></a>
<? endforeach ?>
Upvotes: 0
Reputation: 6909
Actually, that is not going to work, isnce your edit.php will never reach its if statement, as you are not sending $_GET["submit"]. You need to do the following (send the submit variable by setting it to any value)
admin.php
$event_id = $_SESSION['fedit'];
if($_GET['edit'])
{
$url = "edit.php?event_id=". $event_id . "&submit=1";
header("Location: $url");
}
edit.php
if($_GET["Submit"])
{
$val=$_GET['event'];
$remail=$_GET['email'];
$rname = $_GET['ename'];
$sqt=mysql_query("UPDATE users SET NAME='$rname',EMAIL='$remail' WHERE EID ='$val'");
header("Location: admin.php");
}
Upvotes: -1
Reputation:
Preferably do this:
if ($_GET["Submit"]) {
...
// Clean any output
ob_clean();
header("Location: $page");
// Exit the PHP process
exit;
}
Upvotes: 0
Reputation: 523
add action='edit.php' method='get'
to your form tag.
and in edit.php page place your rest of the code after submit.
Upvotes: 0
Reputation: 3852
use header("Location : ". $page);
instead of header("Location: $page");
if($_GET["Submit"])
{
$val=$_GET['event'];
$remail=$_GET['email'];
$rname = $_GET['ename'];
$sqt=mysql_query("UPDATE users SET NAME='$rname',EMAIL='$remail' WHERE EID ='$val'");
$page= "admin.php";
header("Location : ". $page);
}
Upvotes: -1