Log in

View Full Version : Redirect by form entry



tonyking
02-14-2008, 07:14 PM
I am a very new php coder and I was wondering how to accomplish the following using php:

Basically, I want to create a unique page for each of my friends, but instead of a long nasty list of links to their pages, I want one simple form where they would type in something into the box, their last name, email, not really sure what yet.
Then, based on what they entered, the script would redirect to the appropriate page.

I think this would be done with a simple html form "POST" method, to my desired php script. That script would then process the information and based on the variable redirect. Sounds easy but I can't find anything to support this.

Gurus help? lol

Jas
02-14-2008, 09:08 PM
Something like this?:



<?php
if(isset($_POST['Go'])){
$name = strip_tags($_POST['name']);
switch($name){
case "Bob":
$page = 'Bob.html';
break;
case "Bill":
$page = 'Bill.html';
break;
case "Bart":
$page = 'Bart.html';
break;
default:
$page = "select_a_page.html";
break;
}
header("Location:$page");
}
?>

tonyking
02-14-2008, 09:24 PM
I think thats on the right track, I thought it would be pretty simple. If I POST a name to this script, and it matches one of the cases shown it should redirect the page I think. I'll give it a try. I think I also need an else statement in there to redirect to a "no matching" page.

Will play around with it and post again. Thx.

Jas
02-14-2008, 09:31 PM
I think I also need an else statement in there to redirect to a "no matching" page.
That's the miracle of the default case. If none of the other cases match, that one will-- and it redirects back to your form or whatever else you want it to be (if you insert the url, rather than the select_a_page.html :)). I should have commented my script to show you what was happening-- I always forget to do that :(

EDIT: also, that script should only be used for a limited number of people. If you have a lot (30 or 40+), you will want to search arrays instead of using the switch statement.

tonyking
02-14-2008, 09:47 PM
I think I might have a problem with my code. I am using this form:



<form method="post" name="name" action="registry.php">
<input type="text" name="name" class="cleardefault" size="20" value="Enter Name" /><br />
<font size="-2" color="#0099FF">(Use ALL lowercase letters)</font><br /><br />
<input type="submit" name="Submit" value="Submit" />


and this is registry.php


<?php
if(isset($_POST['Go'])){
$name = strip_tags($_POST['name']);
switch($name){
case "Bob":
$page = 'Bob.html';
break;
case "Bill":
$page = 'Bill.html';
break;
case "Bart":
$page = 'Bart.html';
break;
default:
$page = "http://www.google.com";
break;
}
header("Location:$page");
}
?>


Didn't really change anything. When I submit the form it just goes to the blank registry.php page and does not display an error. I am assuming I'm not passing the information to the script correctly?

I am really new to all of this, so it's probably something really simple or stupid, but thanks for your patience and help!

Jas
02-14-2008, 11:01 PM
To tell you how mean I am, I figured you might get stuck there, but I was too lazy to type it out before. Sorry :(

My script assumes that the name of the submit button will be "Go" and the name of the text box will be "name". You can change those here:

if(isset($_POST['YOUR NAME HERE'])){ //name of the submit button
$name = strip_tags($_POST['YOUR NAME HERE']); //name of the textbox

The first if the submit button, the second is the textbox. So:

if(isset($_POST['Submit'])){
$name = strip_tags($_POST['name']);
Or you can change the names in your form.

Explination: Typically in PHP, in order to check if the form was submitted, you look to see if $_POST['submit_button_name'] has a value (isset() function). If it does, the form was submitted. The second line retieves the value of the textbox, and also takes out any html tags (it's good practice for server side scripting).

dcstow
02-14-2008, 11:03 PM
Try changing:

if(isset($_POST['Go'])){

To:

if(isset($_POST['Submit'])){

tonyking
02-14-2008, 11:10 PM
Awesome! That worked perfectly, thanks for all your help. I will try to expand on this now with a few other things, like validation.

I think I'll run into a lot of case-sensitive based issues.

dcstow
02-14-2008, 11:18 PM
I think I'll run into a lot of case-sensitive based issues.

Have a look at these to convert your string ($name) to either all UPPER or all lower case.

http://php.net/strtolower
http://php.net/strtoupper

Jas
02-14-2008, 11:20 PM
Indeed you're both correct. I didn't think of the case. dcstow produced the best way to solve that problem. Just put one of those functions around $_POST['name'] and change the cases of the names in the switch statements (i.e. case "BOB": )

EDIT: That first post in strtoupper is impressive. Converting special chars (don't know why you would need to, but it's nice).

tonyking
02-15-2008, 09:15 PM
Thanks Jas, and dcstow. You've both been very helpful, and I think I've accomplished what I needed. I now have an input box where an inputted string is converted to lowercase, and processed, then redirecting the user to the appropriate page. Awesome!

One thing I am ehh, "semi" worried about was one of Jas' comments about a 30 or 40 case maximum. As I grow these pages, it will inevitably reach that number, and of course, for each new page I must also edit this php page. What are my options to the case method? If every "case" identically matched all my page names, would the "case method" no longer be the logical choice?

Can't I just validate, convert to lowercase, add ".html" and pass the variable directly into the location code? Therefore directing a user to the appropriate page?

Jas
02-15-2008, 09:50 PM
You could, I suppose, but it would be a lot of code. A better way would be something like this:


$names = array();
$urls = array();

$names[] = "BOB"; $urls[] = "bob.html";
$names[] = "BILL"; $urls[] = "bill.html";
$names[] = "BART"; $urls[] = "bart.html";

$page = array_search($name, $names);

if($page == false){
$page = "Somthing.html"; // <--Back to the form, or a not found page
}else{
$page = $urls[$page];
}


It's MUCH smaller then a case for each name. You could also pull the array out of another file, if you ever needed to. It gives you many more options.

EDIT: Oh, or the easier solution:


$names = array();

$names[] = "BOB";
$names[] = "BILL";
$names[] = "BART";

$page = array_search($name, $names);

if($page == false){
$page = "Somthing.html"; // <--Back to the form, or a not found page
}else{
$page = $names[$page].'html';
}

tonyking
03-28-2008, 03:10 PM
This code is now presenting me with a problem in IE, and I can't for the life of me figure out why!? I am using a page called registry.php, here's the code:



<?php
if(isset($_POST['Submit'])){
$name = strip_tags($_POST['name']);
$name = strtolower($name);
switch($name){
case "haynes":
$page = 'haynes.html';
break;
case "bill":
$page = 'bill.html';
break;
case "bart":
$page = 'bart.html';
break;
default:
$page = "noregistry.html";
break;
}
header("Location:$page");
}
?>


And I am using the below form to post to the script.



<form name="Submit" method="post" action="registry.php">
<input type="text" name="name" class="cleardefault" size="20" value="Groom's Last Name" /><br />
<font size="-2" color="#0099FF">(Use ALL lowercase letters)</font><br /><br />
<input type="Submit" name="Submit" value="Submit" />
</form>


It works in firefox, and upon original testing, it worked in IE. Now when I test in IE it takes me to the registry.php page and stops. The script does not redirect to the appropriate case or page.

Here's the live example:
www.damartravel.com/honeymoons.html
You can see the form in the left hand column.

Here's the direct link to the page it should redirect to:
www.damartravel.com/haynes.html (http://www.damartravel.com/haynes.html)

Hopefully this is something silly, and one of you know what it is! lol... It's been driving me crazy all morning.

Thanks in Advance.

Keenan
03-29-2008, 06:46 AM
Hey guys!

The script is working fabulous :)!

Just wondering however is there a code to protect the pages? So they have to of logged in that field to get to Bob.html and so no other users can go directly to Bob.html?

Much appreciated, Keenan.

rangana
03-29-2008, 07:48 AM
@TonyKing,
I'm not fluent in PHP, but as I see, your code has some errors ;)
See the header() function spec (http://us.php.net/header). There should always be http:// :)

Also, I suppose you should post name instead of submit

Try changing your PHPcode to:


<?php
if(isset($_POST['name']))
{
$myweb = 'http://www.damartravel.com';
$name = strip_tags($_POST['name']);
$name = strtolower($name);

switch($name)
{
case "haynes":
$page = $myweb.'/haynes.html';
break;
case "bill":
$page = $myweb.'/bill.html';
break;
case "bart":
$page = $myweb.'/bart.html';
break;
default:
$page = $myweb.'/noregistry.html';
break;
}
header("Location:".$page);
}
?>

See if helps ;)

@Keenan
You should use the forum's search tool, or create a new thread instead, You should'nt hi-jack someone else's thread :)

tonyking
03-30-2008, 03:49 AM
Thanks ragana, your code is far more solid than my original, and I will put it to good use. The "problem" I found with my script(s) was in a javascript, as it only fails when the user pushes enter on the keyboard, instead of clicking submit.

Corresponding thread: click here (http://www.dynamicdrive.com/forums/showthread.php?t=30905)

Definite improvement on the code though! Thanks :)