Apr 272012
 

To just redirect the page in PHP use:

header("Location: http://someAddress");

Now let’s say that after you process a form update, you want to let the user know that the update occurred successfully. The following works for that purpose. I’m going to post it with the functions I use in a Utility class for my own future reference.

First, the functions as used in some PHP file.

 echo Utility::alert("Data successfully updated.");
 echo Utility::redirect(HOME_PAGE."?userId=".Utility::get("userId"));

 

Next, the class with the two functions used in the example.

 class Utility {

  /**
   * Returns html text to perform a javascript alert box displaying text given by
   * $message
   * @param mixed $message - The text to be displayed in the alert box
   * @return string - Returns html text to perform a javascript alert box displaying text  
   * given by $message
   */
  public static function alert($message) {
    $result = "<script type='text/javascript'>alert('$message');</script>";
    return $result;
  }

  /**
   * Essentially the same effect as header("Location".$destination); however this is
   * done with javascript
   * @param mixed $destination - The url to redirect to
   * @return string - Returns the html for a javascript script to redirect the page
   */
  public static function redirect($destination) {
    $result = "<script type='text/javascript'>window.location = \"$destination\";</script>";
    return $result;
  }
 } // end of class

At a later time, I’ll put up my Utility::post($field), Utility::get($field), and Utility::session($field) functions.

 Posted by at 3:42 pm