homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

Redirect / Home Button

 7:31 pm on May 7, 2012 (gmt 0)

I want to add a redirect to the home page on a successful change, after the confirmation message and a redirect to the previous page on an error.

@mysql_select_db($database) or die( "Unable to select database");

$termnew1 = (isset($_POST['termnew']) and ! empty($_POST['termnew']))?mysql_real_escape_string($_POST['termnew']):null;
$ComputerNamenew1 = (isset($_POST['ComputerNamenew']) and ! empty($_POST['ComputerNamenew']))?mysql_real_escape_string($_POST['ComputerNamenew']):null;

if (! ($termnew1 and $ComputerNamenew1 )) { die("Required variables not present"); }

$updated="UPDATE Inventory SET ComputerName = '$ComputerNamenew1' WHERE AssetTag = '$termnew1'";
$result = mysql_query($updated) or die("Cannot execute update: " . mysql_error());

echo("<br>Computer name changed successfully");
} else{





 7:51 pm on May 7, 2012 (gmt 0)

What have you tried?

I don't see any mention of the HEADER directive in your code snippet.


 8:08 pm on May 7, 2012 (gmt 0)

ok, I had tried putting header in the if statement, went back and added it before the echo, and it worked. Can I use the same header option in a button, or is there an easier way to assign a url to a button?


 12:04 am on May 8, 2012 (gmt 0)

You have a number of different options here. 1. You can use a button with an onclick js event to specify a URL. 2. Or you could use and image of a button and link it. 3. Or you could use a meta refresh. 4. Or Use a php header redirect.

Both the button and the image option would need to show once your script has ran allowing the user to then click the button. A header redirect immediately redirects without any display. And a meta refresh can allow an echo statement to be shown for a predetermined amount of seconds, then would automatically refresh the page to the predetermined URL. Which sounds best and I'll help you write the code :-) or what did I confuse you on? Lol!


 4:56 pm on May 8, 2012 (gmt 0)

Iwant to add a redirect to the home page on a successful change, after the confirmation message and a redirect to the previous page on an error.

allenflame are you fairly new at PHP? If so, break this habit now, while you're still learning.

A redirect on error of submtting any form is **awful** in terms of usability and "user-friendliness." It leaves the form fields empty, and what a lot of programmers do is to "patch" this by storing everything in session variables and if there's an error, repopulate the fields with these variables. It does work, but it's a lot of extra programming than if you reviewed your logic and planned it out so that your program allows for the potential of errors. Like so.

// Do not use @, this supresses errors. Error trapping is your friend.
mysql_select_db($database) or die( "Unable to select database");
if ($_POST['some_form_value']) {
$errors = error_checking_function();
if ($errors) { output_form_function($errors); }
else {
else { output_form_function(null); }

That's your entire program. By planning for the potential of errors, you're planning for ways to handle it that are easy for the user. What would those functions look like? This is a simple one-off and incomplete, but something like so:

function error_checking_function() {
$errors = null;
// The associative array allows us to assign "plain English"
// values to the fields. It really should be a global
// array passed to the function, as it can be used in
// process_form below.
$requireds = array(
'termnew' => 'New Term',
'ComputerNamenew' = 'New Computer Name'
foreach ($requireds as $req => $english) {
if (! isset($_POST[$req]) or (isset($_POST[$req]) and empty($_POST[$req]))) {
$errors .= "<li>The $english field is required.</li>\n";
return $errors;

function output_form_function($errs) {
// You'd use a template system or output full HTML here.
// These variables should be in an array, but anyway . . .
$tn = (isset($_POST['termnew']))?$_POST['termnew']:null;
$cn = (isset($_POST['ComputerNamenew']))?$_POST[ComputerNamenew']:null;
$form = '<form method="post" action="yourscript.php">';
if ($errs) {
$form .= '<p class="errors">There are some errors in your form:</p><ul>' .
$errs . '</ul>';
$form .= '<p><Label for="termnew">New Term</label>
<input type="text" name="termnew" id="termnew" value="' . $tn . '"></p>
<p><Label for="ComputerNamenew">New Computer Name</label>
<input type="text" name="ComputerNamenew" id="ComputerNamenew" value="' . $tn . '"></p>
<p><input type="submit" value="Submit"></p>
echo $form;

function process_form_function() {
// We've already checked if they are empty so . . . .
$updated="UPDATE Inventory SET ComputerName = '" .
mysql_real_escape_string($_POST['ComputerNamenew']) . "' WHERE AssetTag = '" .
mysql_real_escape_string($_POST['termnew']). "'";
$result = mysql_query($updated) or die("Cannot execute update: " . mysql_error());

function output_success_message() {
// Customize as needed, see output form above.
echo 'Ta dah.';

This is not copy and paste code but gives you an idea of how you can create a full circle, single file, self contained program instead of multiple files that often make more work for you.

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved