I'm trying to get my page to redirect back to another page after the form has been submitted to its self via $_SERVER[PHP_SELF] . I've tried using the header(Location) script but to no avail. Anyone know of an easy way to do this?

Right now.. when my page is submitted all the action and everything is done properly but I'm left with a blank white page.

Here's the code..

Thanks in advance.

<!DOCTYPE html PUBLIC "-//D XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<?php
include('../inc/storeinfo.php');
include('../database.php');

$noteid=$_POST["noteid"];

?>

<head>
<Title><? print $storename?> - <? print $sitename?> - Home</Title>	
<link rel="stylesheet" href="/fancybox/jquery.fancybox-1.3.4.css" type="text/css" media="screen" />
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>
<script type="text/javascript" src="/fancybox/jquery.fancybox-1.3.4.pack.js"></script>
<link href="../css/viewtickets.css" rel="stylesheet" type="text/css">
</head>
<body>

<div id="editNotesContainer">
	
<?	
$result = mysql_query("SELECT * FROM info WHERE ticket='$noteid'");

while($row = mysql_fetch_array($result))
  {
  echo 	"<div id=\"editNotesHeader\">Editing Notes for Ticket #$noteid</div>" .  	
  		"<div id=\"editNotes\">" .
  		"<form method=\"post\" action=\"$_SERVER[PHP_SELF]\">" . 
  		"<textarea name=\"newnote\" rows=\"10\" cols=\"48\">{$row['notes']}</textarea>" .
  		"<input type=\"hidden\" value=\"$noteid\" name=\"newnoteid\">" .
  		"<br /><input type=\"submit\" value=\"Update\">" .
  		"</form>" .
  		"</div>" ;
  }

if(isset($_POST)){
	$newnote=$_POST["newnote"];		
	$newnoteid=$_POST["newnoteid"];
	mysql_query("UPDATE info SET notes = '$newnote' WHERE ticket = '$newnoteid'");
    echo "testing";
}

?>
</div>
</body>
</html>
<?
mysql_close($con);
?>

Dani AI

Generated

Good call by to keep submission logic before output, and was right to point out Post/Redirect/Get (PRG) to avoid refresh/resubmit problems. For a clean, reliable flow: detect POST, perform the update with a safe DB API, then send a redirect and stop execution. Use a specific check (not just isset($_POST)), send a proper Location header (preferably with status 303), and call exit so nothing else outputs.

Example pattern (safe, minimal):

<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['newnote'], $_POST['newnoteid'])) {
    // use PDO or mysqli with prepared statements (example uses PDO)
    $pdo = new PDO('mysql:host=HOST;dbname=DB;charset=utf8mb4', 'USER', 'PASS', [
        PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
    ]);
    $stmt = $pdo->prepare('UPDATE info SET notes = :note WHERE ticket = :ticket');
    $stmt->execute([':note' => $_POST['newnote'], ':ticket' => $_POST['newnoteid']]);

    // redirect to a results or view page (303 avoids resubmit on refresh)
    header('Location: /viewticket.php?ticket=' . urlencode($_POST['newnoteid']), true, 303);
    exit;
}
?>

Troubleshooting notes: enable error reporting while debugging (ini_set('display_errors',1); error_reporting(E_ALL);), check headers_sent() if you get "headers already sent", and avoid UTF-8 BOM or stray whitespace before <?php. Prefer PDO/mysqli over deprecated mysql_* functions and always validate/sanitize inputs. 's solution of posting directly to the handler page is fine — it follows the same idea: process first, then redirect or render a confirmation.

Recommended Answers

All 6 Replies

where did you put the

header(Location)

? It needs to be before any HTML

Member Avatar for Member #120589

Just an obs - send forms to different pages, like form handlers, or you'll get trouble when users refresh/reload.

where did you put the

header(Location)

? It needs to be before any HTML

Thing is.. it can't be before my HTML because then the page wont load at all, right? It has to load the page once initially so that the form in it can submit to itself.

Just an obs - send forms to different pages, like form handlers, or you'll get trouble when users refresh/reload.

Thanks for the tip. I'm attempting to make this work so that it cuts down on the pages and code I use, and the amount of redirects. If I can get it to work I'll only have 2 instead of 3.

Thing is.. it can't be before my HTML because then the page wont load at all, right? It has to load the page once initially so that the form in it can submit to itself.

You can make a POSTed variable that checks if the form was submitted (such as a hidden form value) and then submits the form and then put the header(Location) line

basically just put your form submission code at the beginning of the file:

<?php
if(isset($_POST)){
	$newnote=$_POST["newnote"];		
	$newnoteid=$_POST["newnoteid"];
	mysql_query("UPDATE info SET notes = '$newnote' WHERE ticket = '$newnoteid'");
        header(Location)
}
?>

the code will still execute on the server and submit the page, the user doesn't actually have to load the page to submit the form as the php is processed by the server

Problem solved. I ended up placing the code on the top of the page I was trying to redirect to, and removed the header(Location) line. That way my action is set to that page, so I end up there.. and the form is submitted on that page as well.

In a way it's a mixture of burgercho's idea and my own!

Thanks for the help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.