Web Hosting Talk







View Full Version : Loading Another Page From PHP


bvanderwerf
09-01-2002, 09:20 PM
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

shaunewing
09-01-2002, 10:50 PM
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.


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

mind21_98
09-01-2002, 11:31 PM
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();
?>

:: paVel ::
09-01-2002, 11:33 PM
Hey,
Thats right, u can use Header() function to redirect users...
Here is some code that would check the fields in one script:

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!;)

apokalyptik
09-02-2002, 05:08 AM
Theres a really easy way to do that, condider:

index.html

<html>
<body>
<form method="get" action="select.php">
<input type="text" name="page" value="load.php">
<input type="submit">
</form>
</body>
</html>


select.php

<?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');
}
?>

dreamrae.com
09-02-2002, 08:31 AM
remember, cookies before using header(char i) :stickout