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

Thread: Simple PHP Validation Help?

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

    Default Simple PHP Validation Help?

    Hello, I have a very simple form where a user will enter a "code". I want to check that code to make sure it matches a list of codes. If it does the code will be emailed to me, and they would be redirected to a success page. If it doesn't match one of the valid codes, they would be redirected to an error page.

    Here's my code so far:
    PHP Code:
    <?php
    $to 
    "my email";
    $subject "Code";
    $email "my email";
    $message $_REQUEST['code'] ;
    $headers "From: $email";
    $sent mail($to$subject$message$headers) ;
    if(
    $sent)
    {
    header'Location: http://www.mysite.com/success.html' ) ; }
    else
    {
    header'Location: http://www.mysite.com/error.html' ) ; }
    ?>
    As you can see no validation of the submitted code, I have no idea how to do this with php, but I know it's not hard.

    All I really need is something simple where I can manually code 15 - 20 codes into the form and check for a match. Can anyone help?

  2. #2
    Join Date
    Nov 2007
    Location
    USA
    Posts
    170
    Thanks
    8
    Thanked 22 Times in 22 Posts

    Default

    Are you going to be storing the codes in a database?
    What is obvious to you might not be to another.


    My Site

  3. The Following User Says Thank You to Moshambi For This Useful Post:

    tonyking (11-02-2008)

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

    Default

    Eventually yes, but for right now I don't mind hard coding 10 into the script itself just to get me by.

  5. #4
    Join Date
    Nov 2007
    Location
    USA
    Posts
    170
    Thanks
    8
    Thanked 22 Times in 22 Posts

    Default

    Well first you need to set up a form:

    Code:
    <form action="submitcode.php" method="post">
    <input type="text" name="usercode" /><br />
    <input type="submit" name="submit" />
    </form>
    then you are going to need to make a check in your PHP file submitcode.php for this example:

    Code:
    $usercode = $_POST['usercode'];
    $realcode = "123abc";
    
    if(strcasecmp($usercode, $realcode) == 0)
    {
    echo "They Match!";
    }
    else
    {
    echo "Invalid code!";
    }
    I would also like to note a few things.

    FIRST! I hvae not tested this code I simply just created it in the reply box just now.

    Second. The strcasecmp() will match a string "XXAA" to "xxAa"....it is case insensitive.

    Third. If you are going to do this from a database there will be some different code.

    I hope this basic example gives you an idea how to go about this, also I hope this post helped you out

    [/code]
    What is obvious to you might not be to another.


    My Site

  6. The Following User Says Thank You to Moshambi For This Useful Post:

    tonyking (11-02-2008)

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

    Default

    Thanks, one question as I don't know the syntax. How do I code it for multiple "real codes"? There will be 10 or so.

  8. #6
    Join Date
    Nov 2007
    Location
    USA
    Posts
    170
    Thanks
    8
    Thanked 22 Times in 22 Posts

    Default

    You could easily do it using an array:

    Code:
    $realcode = array('code1', 'code2', 'code3', 'code4', 'code5'); // You can put as many as you need in here...
    
    $match = "false";
    // Now to loop through the array...
    foreach ($codes as $value)
    {
    if(strcasecmp($realcode[$value], $usercode) == 0)
    {
    $match = "true";
    }
    }
    
    if($match == "true")
    {
    echo "Valid Code!";
    }
    else
    {
    echo "Invalid Code!";
    }
    What is obvious to you might not be to another.


    My Site

  9. The Following User Says Thank You to Moshambi For This Useful Post:

    tonyking (11-02-2008)

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

    Default

    Awesome I think that's exactly what I need, then I can just replace the "Valid Code" message with an email script & redirect.

  11. #8
    Join Date
    Nov 2007
    Location
    USA
    Posts
    170
    Thanks
    8
    Thanked 22 Times in 22 Posts

    Default

    Yup you should be able to. But if you have troubles with it you can always post here hope it all goes well for you
    What is obvious to you might not be to another.


    My Site

  12. The Following User Says Thank You to Moshambi For This Useful Post:

    tonyking (11-02-2008)

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

    Default

    Or not.. >.< Wish I knew this stuff better. here's the code I am using:
    PHP Code:
    <?php
    $usercode 
    $_POST['code'];
    $codes = array('1''2''3''4''5');
    $match "false";

    foreach (
    $codes as $value)
    {
    if(
    strcasecmp($realcode[$value], $usercode) == 0)
    {
    $match "true";
    }
    }

    if(
    $match == "true")
    {
            
    $to "info@n2flash.com";
            
    $subject "Raffle Code";
            
    $email "info@n2flash.com";
            
    $message $_REQUEST['code'] ;
            
    $headers "From: $email";
            
    $sent mail($to$subject$message$headers) ;
            if(
    $sent)
            {
    header'Location: http://www.n2flash.com/success.html' ) ; }
            else
            {
    header'Location: http://www.n2flash.com/error.html' ) ; }
    }
    else
    {
    header'Location: http://www.n2flash.com/error.html' ) ;
    }

    ?>
    If i submit a 1, 2 ,3, 4 or 5 or anything else... It directs to the error page, and no email is sent.

    If I leave the form blank, it goes to success and sends an email. Is there somethign wrong with what I have?

  14. #10
    Join Date
    Nov 2007
    Location
    USA
    Posts
    170
    Thanks
    8
    Thanked 22 Times in 22 Posts

    Default

    Ok 1 thing that might be making it mess up...but I'm not positive about this is where you do your foreach loop try using a different variable for $codes as that is what you named your array...

    if that doesn't work could you show your html page too please?


    EDIT: I just noticed that you should just name you array to $realcodes for this line here:
    if(strcasecmp($realcode[$value], $usercode) == 0)
    What is obvious to you might not be to another.


    My Site

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
  •