Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Jun 2008
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts

    If Statement That Runs On Form Submit

    I have a real simple form where the user enters a word and hits submit. The word is then appended to the end of a url that I have in the code and redirects to the new url. The script I have is working but the only thing I can't figure out how to do is when it hits submit I want it to run an if statement first that checks the word they entered and if it's the exact match to a word we have in the if statement it redirects. If it doesn't match it says a line of text under the text box "Sorry that word is incorrect". Here's the working code. Any help would be GREATLY appreciated!

    <?php
    if (isset($_POST['bt']))
    {
    header("Location: http://www.google.com/" . $_POST['folder']);
    }
    ?>
    <html>
    <form id="form1" name="form1" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
    <input type="text" name="folder" id="folder" />
    <input type="submit" name="bt" id="bt" value="Go To" />
    </form>
    </html>

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    PHP Code:
    <?php
    // if (isset($_POST['bt'])) // never do this.
    $sCompareWord 'COMPAREWORD';
    $sError '';
    if (isset(
    $_POST['folder']) && strcmp($_POST['folder'], $sCompareWord) == 0)
    {
        
    header('Location: http://www.google.com/' $sCompareWord);
        exit();
    }
    else
    {
        
    $sError printf("<div>%s is not the correct word</div>" PHP_EOLhtmlspecialchars($_POST['folder']));
    }
    ?>

    <html>
    <?php echo $sError;?>
    <form id="form1" name="form1" method="post" action="<?php echo $_SERVER['SCRIPT_NAME']?>">
    <input type="text" name="folder" id="folder" />
    <input type="submit" name="bt" id="bt" value="Go To" />
    </form>
    </html>
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #3
    Senior Coder
    Join Date
    Sep 2010
    Posts
    1,899
    Thanks
    15
    Thanked 226 Times in 226 Posts
    PHP Code:

    <?php
    if (isset($_POST['folder']))
    {
    $flag=0;
    if ( 
    $_POST['folder']=='whoever' )
    {
    header("Location: http://www.google.com/" $_POST['folder']);
    }
    else 
    $flag=1;
    ?>
    <html>
    <form id="form1" name="form1" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
    <input type="text" name="folder" id="folder" />
    <?php if (isset($flag) && $flag==1) echo "Sorry that word is incorrect"?>
    <input type="submit" value="Go To" />
    </form>
    </html>
    Welcome to http://www.myphotowizard.net

    where you can edit images, make a photo calendar, add text to images, and do much more.


    When you know what you're doing it's called Engineering, when you don't know, it's called Research and Development. And you can always charge more for Research and Development.

  • #4
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    3,763
    Thanks
    23
    Thanked 548 Times in 547 Posts
    LOL guess I'll just post mine to add to the confusion:
    PHP Code:
    <?php
    $word 
    "Enter a car name ";
    $cars = array("volvo","bmw","toyota");


    if (isset(
    $_POST['bt']))
    {
        if(
    in_array(strtolower($_POST["folder"]), $cars)){
            
    header("Location: http://www.google.com/" $_POST['folder']);
        }else{
            
    $word "Fill this out correctly";
        }
    }
    ?>

    <!DOCTYPE html>
    <html>
    <head>
    <title>Title of the document</title>
    </head>

    <body>
    <form id="form1" name="form1" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" >
    <input required type="text" name="folder" id="folder" placeholder="<?php echo $word ?>">
    <input type="submit" name="bt" id="bt" value="Go To" />
    </form>
    </body>

    </html>
    Evolution - The non-random survival of random variants.


  •  

    Tags for this Thread

    Posting Permissions

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