Page 1 of 2 12 LastLast
Results 1 to 10 of 16

Thread: Redirect by form entry

  1. #1
    Join Date
    Jan 2007
    Posts
    82
    Thanks
    30
    Thanked 18 Times in 17 Posts

    Default Redirect by form entry

    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
    Last edited by tonyking; 02-14-2008 at 08:33 PM.

  2. #2
    Join Date
    Jan 2007
    Posts
    629
    Thanks
    10
    Thanked 28 Times in 28 Posts

    Default

    Something like this?:

    PHP Code:
    <?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");
        }
    ?>
    --Jas
    function GreatMinds(){ return "Think Like Jas"; }
    I'm gone for a while, but in the meantime: Try using my FTP script | Fight Bot Form Submissions

  3. #3
    Join Date
    Jan 2007
    Posts
    82
    Thanks
    30
    Thanked 18 Times in 17 Posts

    Default

    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.

  4. #4
    Join Date
    Jan 2007
    Posts
    629
    Thanks
    10
    Thanked 28 Times in 28 Posts

    Default

    Quote Originally Posted by tonyking View Post
    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.
    Last edited by Jas; 02-14-2008 at 09:40 PM.
    --Jas
    function GreatMinds(){ return "Think Like Jas"; }
    I'm gone for a while, but in the meantime: Try using my FTP script | Fight Bot Form Submissions

  5. #5
    Join Date
    Jan 2007
    Posts
    82
    Thanks
    30
    Thanked 18 Times in 17 Posts

    Default

    I think I might have a problem with my code. I am using this form:

    HTML Code:
    <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 Code:
    <?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!

  6. #6
    Join Date
    Jan 2007
    Posts
    629
    Thanks
    10
    Thanked 28 Times in 28 Posts

    Default

    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:
    PHP Code:
        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:
    Code:
        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).
    Last edited by Jas; 02-14-2008 at 11:07 PM.
    --Jas
    function GreatMinds(){ return "Think Like Jas"; }
    I'm gone for a while, but in the meantime: Try using my FTP script | Fight Bot Form Submissions

  7. #7
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Try changing:
    Code:
    if(isset($_POST['Go'])){
    To:
    Code:
    if(isset($_POST['Submit'])){

  8. #8
    Join Date
    Jan 2007
    Posts
    82
    Thanks
    30
    Thanked 18 Times in 17 Posts

    Default

    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.

  9. #9
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by tonyking View Post
    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

  10. #10
    Join Date
    Jan 2007
    Posts
    629
    Thanks
    10
    Thanked 28 Times in 28 Posts

    Default

    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).
    Last edited by Jas; 02-14-2008 at 11:29 PM.
    --Jas
    function GreatMinds(){ return "Think Like Jas"; }
    I'm gone for a while, but in the meantime: Try using my FTP script | Fight Bot Form Submissions

Bookmarks

Posting Permissions

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