Results 1 to 6 of 6
  1. #1
    Join Date
    Aug 2002
    Posts
    35

    Loading Another Page From PHP

    I guess I should preface this by saying I am using the methodology of creating static HTML pages and sprinkling in PHP code as necessary. I know this may be considered Bad Practice, but I like using Dreamweaver templates and laying out my screens in a WYSIWYG editor.

    So, using this methodogy, how does one load a specific page based on the results of a form?

    Take, for example, a customer login screen. When the form is submitted, one of several pages might be presented to the user: an entry form if this is a new user, the desired page if the login is successful, the login form again (with an error message) if the e-mail can't be found or the password is bad, or an acknowledgement page if the customer requests the password be e-mailed to them.

    In the script called when the form is submitted, I've been using a JavaScript window.location call to the right page, depending on what the customer selected, valid data, etc. It occurs to me that this is probably not the best solution (e.g. the user can turn JavaScript off), but what is?

    Any feedback on my this subject would be appreciated.

    --Bruce

  2. #2
    Join Date
    Jun 2002
    Location
    Sydney, Australia
    Posts
    1,745
    You could use the Header to redirect the person dependent on variables.

    I'll give a generic example using the variable $usrType which you can then alter to suit your needs.

    PHP Code:
    if($usrType == "newuser"
    {
      
    Header("Location: http://www.yoursite.com/newuser.php");
    }
    elseif(
    $usrType == "verified")
    {
      
    Header("Location: http://www.yoursite.com/verified.php");
    }
    elseif(
    $usrType == "badpwd")
    {
      
    Header("Location: http://www.yoursite.com/login.php");
    }
    else
    {
      
    Header("Location: http://www.yoursite.com/unknown.php");

    Basically this shows how you can redirect to different pages depending on a certain condition which is what I assume you're after.

    I should point out that the Header tag *must* be on your page before any text has been output to the user.

    I hope that helps.

    --Shaun
    Shaun Ewing
    http://shaun.net

  3. #3
    Join Date
    Dec 2000
    Location
    San Diego, CA
    Posts
    1,571
    If you use output buffering, you can put Header() anywhere, provided that it's in between ob_start() and ob_end_flush(). For instance:

    <?php
    ob_start();
    echo "test text";
    Header("Location: http://www.microsoft.com/");
    ob_end_flush();
    ?>
    -Mooneer
    Thoughtbug Software: Hosting shouldn't require any thought.
    Legitimate host? Support the Code of Ethical Conduct

  4. #4
    Join Date
    Jul 2002
    Location
    Australia
    Posts
    324
    Hey,
    Thats right, u can use Header() function to redirect users...
    Here is some code that would check the fields in one script:
    PHP Code:
    if (!$username
    {
       echo 
    "USERNAME is empty"
    }
    else if (!
    preg_match("/^[a-zA-Z0-9_-]+$/"$username))
    {
       echo 
    "Username can only contain latters and numbers";
    }
    else if (
    strlen($username)>20 || strlen($username)<3
    {
       echo 
    "Username must be between 3-20 characters";
    }
    else if (
    $pass!=$cpass
    {
       echo 
    "Passwords entered were not the same";
    }
    else if (!
    eregi("^[0-9a-z]([-_.]?[0-9a-z])*@[0-9a-z]([-.]?[0-9a-z])*\\.[a-z]{2,3}$",$email) && $email != ""
    {
       echo 
    "E-mail adress that you entered is not valid";
    }
    else 
    // if everything is OK
    {
       
    // DO something, if everything is correct.!

    Hope that helps!

  5. #5
    Theres a really easy way to do that, condider:

    index.html
    Code:
    <html>
    <body>
    <form method="get" action="select.php">
    <input type="text" name="page" value="load.php">
    <input type="submit">
    </form>
    </body>
    </html>
    select.php
    Code:
    <?php
    switch ( $page ) {
     case 'info':
      include('./info.php');
      break;
     case 'menu':
      include('menu.php');
      break;
     case 'static':
      include('./static.html');
      break;
     default:
      include('index.html');
    }
    ?>
    irc.apokalyptik.com http://www.apokalyptik.com http://www.apokalyptik.com/forum/

  6. #6
    Join Date
    Aug 2002
    Location
    Baltimore, Maryland
    Posts
    580
    remember, cookies before using header(char i)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •