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 14 of 14
  1. #1
    New Coder
    Join Date
    Jul 2006
    Posts
    60
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Where's The Error?

    PHP Code:
    $user $_POST['user'];
    $score1 $_POST['score1'];
    $score2 $_POST['score2'];

            if (empty(
    $user) || empty($score1) || empty($score2))
            {
            echo 
    '<div align="center" <span class="style1"><strong>ERROR:</strong> You left one or more fields empty, please complete all fields and try again.</span></div>';
            include 
    'prediction+league.php';
        exit();
            }
            else
            
    $message "Craig...
    $score1
    $score2"
    ;

    mail("craig.howarth@toon-army.co.uk""Predictions",
    $message"From: $user");
    echo 
    '<div align="center" <span class="style1">Your predictions have been sent successfully.</span></div>'
    The code doesn't work, no matter what if all the fields have text in it still brings up the error message, but still sends the information to the email address.

    Thanks in advance.

    Craig

  • #2
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,018
    Thanks
    2
    Thanked 313 Times in 305 Posts
    About the only way it would display the "ERROR: You left one or more fields ..." message and send an email is if the include file contains code that sends an email.
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #3
    New Coder
    Join Date
    Nov 2005
    Posts
    97
    Thanks
    1
    Thanked 0 Times in 0 Posts
    to start the syntax is incorrect
    try the testing bits below.. and check that 'empty' is not displayed.

    PHP Code:
    $user $_POST['user'];
    $score1 $_POST['score1'];
    $score2 $_POST['score2'];

    //testing
    echo $user;
    if(empty(
    $user)) echo "empty";
    echo 
    $score1;
    if(empty(
    $score1)) echo "empty";
    echo 
    $score2;
    if(empty(
    $score2)) echo "empty";
    //

    if (empty($user) || empty($score1) || empty($score2)){
      echo 
    '<div align="center" <span class="style1"><strong>ERROR:</strong> You left one or more fields empty, please complete all fields and try again.</span></div>';
      include 
    'prediction+league.php';
      exit();
    }else{
      
    $message "Craig... 
      $score1
      $score2"
    ;

    mail("craig.howarth@toon-army.co.uk""Predictions",
    $message"From: $user");
    echo 
    '<div align="center" <span class="style1">Your predictions have been sent successfully.</span></div>';

    Last edited by alexpayne; 11-04-2006 at 03:10 PM.
    Edit: Nothing that can be considered offensive in the signature, please. // liorean

    www.moresense.co.uk

  • #4
    Registered User
    Join Date
    Oct 2004
    Posts
    592
    Thanks
    0
    Thanked 1 Time in 1 Post
    why not isset()?

  • #5
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    The trick with empty() is it will return TRUE if the value you send is 0 or "0". This is just a guess but based on your variable names ($score1 and $score2) it's likely one of those values is "0" and therefore doesn't pass the empty() test the way you'd expect.

    The reason the mail is being sent is because you didn't wrap your code block up with brackets {} in the "else" logic so the only statement that gets run with the "else" statement is the $message assignment. Everything below that will be run every time.

  • #6
    New Coder
    Join Date
    Jul 2006
    Posts
    60
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Fumigator View Post
    The trick with empty() is it will return TRUE if the value you send is 0 or "0". This is just a guess but based on your variable names ($score1 and $score2) it's likely one of those values is "0" and therefore doesn't pass the empty() test the way you'd expect.

    The reason the mail is being sent is because you didn't wrap your code block up with brackets {} in the "else" logic so the only statement that gets run with the "else" statement is the $message assignment. Everything below that will be run every time.
    So how will i get around that?

    What shall i put instead of empty()?

  • #7
    Senior Coder rafiki's Avatar
    Join Date
    Aug 2006
    Location
    Floating around somewhere...
    Posts
    2,042
    Thanks
    19
    Thanked 42 Times in 42 Posts
    would this work
    PHP Code:
    $str '$score1';
    if (
    strlen($str) < 1) {
    // your code here for score existing 
    else 
    {
    echo
    " You missed out a score";


  • #8
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    Yep you could use strlen($str) > 1, or if($str > '') is probably another safe bet.

  • #9
    New Coder
    Join Date
    Jul 2006
    Posts
    60
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Sorry i'm abit new (and thick) to PHP, could you add the code needed into mine.

    Cheers

  • #10
    Senior Coder rafiki's Avatar
    Join Date
    Aug 2006
    Location
    Floating around somewhere...
    Posts
    2,042
    Thanks
    19
    Thanked 42 Times in 42 Posts
    Hi craig,
    heres a script i wrote up for you, should work and you may need 2 make minor changes.

    PHP Code:

    <?php 

    $user 
    $_POST['user'];
    $score1 $_POST['score1'];
    $score2 $_POST['score2'];

    $str1 $user;
    $str2 $score1;
    $str3 $score2;

    if(
    strlen(str1str2str3 0)) //check the amount of chars in $user, $score1 and $score2
    {
    echo 
    "You missed some information"//your error if there is no info entered
    }
    else 
    {
    $message "Craig ....
    $score1
    $score2"
    ;
    mail(craig.howarth@toon-army.co.ukPredictions$messageFrom$user,)
    }

    if (
    mail(craig.howarth@toon-army.co.ukPredictions$messageFrom$user,))
    {
    echo 
    " Your predictions have been sent successfully";
    }
    else
    {
    echo 
    "An error occured please go back and try again";
    }
    ?>
    Positive rep appreciated :P

  • #11
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    PHP Code:
    if(strlen(str1str2str3 0)) 
    You really think that will work?

  • #12
    Senior Coder rafiki's Avatar
    Join Date
    Aug 2006
    Location
    Floating around somewhere...
    Posts
    2,042
    Thanks
    19
    Thanked 42 Times in 42 Posts
    cant you check multiple variables within the 1 function?

  • #13
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    Don't you think you'd want to find out for sure before you post the code as a solution?

  • #14
    Senior Coder rafiki's Avatar
    Join Date
    Aug 2006
    Location
    Floating around somewhere...
    Posts
    2,042
    Thanks
    19
    Thanked 42 Times in 42 Posts
    i was in bed when i write the script couldnt get php.net on my fone and couldnt be bothered to google it lol, sorry


  •  

    Posting Permissions

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