I have a site that allows users to edit the text on a page. When they go to edit, I grab the page id, the users make their changes, and submit. Everything is hunky dory, but I can't figure out how to redirect them back to the page they came from to see their changes.
On any given page, an authorized user I have something like:
<a href="editcontent.php?&content_id=<?php echo $content_id; ?>"edit page</a>
where $content_id = the current page id.
Then, I have another page with the form and code for updating the db. The first section is what I need help with. After the successful query, I need to direct them back to whatever page they requested the edit form from:
<?php
include 'data.php';
if (!isset($_SESSION["loggedIn"]) || $_SESSION["loggedIn"] != true ) {
header("Location: login.php");
}
if(isset($_POST["submit"])) {
$pagetext=$_POST['elm1'];
$content_id=$_POST['content_id'];
$sql = "UPDATE content SET pagetext='$pagetext' WHERE content_id='$content_id'";
$result = mysql_query($sql)
or die("Invalid query: " . mysql_error());
$ref = $_SERVER['HTTP_REFERER'];
header("Location: $ref");
}
?>
<?php
$contentsql = "SELECT pagetext FROM content
WHERE content_id = '" . $_GET['content_id'] . "'";
$result = mysql_query($contentsql)
or die("Invalid query: " . mysql_error());
$row = mysql_fetch_array($result);
$pagetext = $row['pagetext'];
?>
<form action="<?=$_SERVER["PHP_SELF"]?>" method="POST">
<input type="hidden" name="content_id" value="<?php echo $_GET['content_id']; ?>">
<div id="formlabel">Page Text:</div><textarea name="elm1" id="formfield" cols="50" rows="25"><?php echo $pagetext; ?></textarea>
<input type="submit" name="submit" id="formbutton" value="Update">
</form>
If you see anything else I can improve, comments welcome.