0

I'm having a problem with my php code.

I don't want the else echo "Check it again!"; to show unless they input some data into the form(input box) and it's not valid. But when I load the page it shows the error above the box.

<?PHP
if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
    mysql_query("INSERT INTO newsletter (email) VALUES('$email')") or die(mysql_error());
        echo 'You have registered your E-Mail address to our database! You will now receive regular updates on the progess!';
    }else{
        echo "Check it again!";
}
?>

<form name="newsletter" method="post" action="<?PHP $_SERVER['PHP_SELF']?>">
<input type="text" name="newsletter" id="newsletter">
<input type="submit" value="SUBMIT!">
</form>
3
  • So where is $email defined? Commented Jul 8, 2013 at 23:05
  • include('./_inc/config.php'); $email = $_POST['newsletter']; $email = stripslashes($email); Commented Jul 8, 2013 at 23:05
  • Do a check for if a post field is set first. This is basic PHP and you will get a few million results from a google search... if( isset($_POST['newsletter']) ) Commented Jul 8, 2013 at 23:06

2 Answers 2

2

Try this :)

<?PHP
if($_SERVER["REQUEST_METHOD"]=="POST"){
    if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
        mysql_query("INSERT INTO newsletter (email) VALUES('$email')") or die(mysql_error());
            echo 'You have registered your E-Mail address to our database! You will now receive regular updates on the progess!';
        }else{
            echo "Check it again!";
    }
}
?>

instead of

<?PHP

 if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
    mysql_query("INSERT INTO newsletter (email) VALUES('$email')") or die(mysql_error());
    echo 'You have registered your E-Mail address to our database! You will now receive regular updates on the progess!';
    }else{
      echo "Check it again!";
    }
}

?>

Logic behind the edit: We'll check for the email or print "Check it again!" only if the form is submitted. Now if we don't check whether the form is submitted or this is the first time loading (or simple refresh) of the page, either insertion or the error display will occur. And we don't want that :)

Sign up to request clarification or add additional context in comments.

Comments

0

This is just an alternative to @MuhammedHedayet answer in the style I prefer, checking it only when you need to. Both should work perfectly:

<?PHP
if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
    mysql_query("INSERT INTO newsletter (email) VALUES('$email')") or die(mysql_error());
        echo 'You have registered your E-Mail address to our database! You will now receive regular updates on the progess!';
    } else if ($_SERVER["REQUEST_METHOD"]=="POST"){
        echo "Check it again!";
}
?>

<form name="newsletter" method="post" action="<?PHP $_SERVER['PHP_SELF']?>">
<input type="text" name="newsletter" id="newsletter">
<input type="submit" value="SUBMIT!">
</form>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.