1

Trying to get the error messages of $missing to appear in the form div with id error, and if the form is filled out correctly display the $complete message into div with id success.

<?php 
$error = false;
$missing = "";
if ($_POST) {      
  $complete = "<p><strong>Thank you!</strong> Your message was sent, 
  we'll get back to you ASAP!</p>";    

   if (filter_var($_POST["email"], FILTER_VALIDATE_EMAIL) === false {        
        $missing .= "A vaild email address is required.<br>";
        $error = true;
    } 
    if (!$_POST['subject']) {
        $missing .= "A subject field is required.<br>";
        $error = true;
    }
    if (!$_POST['content']) {
        $missing .= "A content field is required.<br>";
        $error = true;
    }
    if ($error) {
        $missing = "<p>". $missing."</p>";
    } else {
        $complete;
    }
} 
?>

This is the HTML form where trying to display.

<form method="post">
     <h1>Get in touch!</h1>      
     <div id="error" class="alert alert-danger" role="alert">
          <? echo $missing; ?></div>
     <div id="success" class="alert alert-success" role="alert"><? echo $complete; ?></div>

Can't really see where i'm going wrong here, any help would be amazing. Thanks.

6
  • 1
    Do you have short tags enabled? Whats the jQuery usage here? Commented Aug 2, 2017 at 3:44
  • 1
    For one thing, this is just a stray variable else { $complete; - and there's code missing in here, as in the (form) elements inputs and the closing form </form> tag. So we don't know if that's at fault or not. This in addition to Chris' comment. Commented Aug 2, 2017 at 3:48
  • Up until we know exactly which animal(s) we're dealing with here, there's really nothing anyone can do for you except to tell you to check your developer console and use error reporting. Commented Aug 2, 2017 at 3:51
  • if (filter_var($_POST["email"], FILTER_VALIDATE_EMAIL) === false ) missing this bracket{ $missing .= "A vaild email address is required.<br>"; $error = true; } Commented Aug 2, 2017 at 5:44
  • Provide your Complete Code Commented Aug 2, 2017 at 5:45

4 Answers 4

2

<? does not mean anything in PHP (unless you have enabled short open tags in your php.ini file). It's usage is discouraged, as it can be disabled - maybe this is what happened here.

There are two other starting tags for PHP, <?php and <?= (the latter of which is only for echoing, and the one that I suspect you want to use).

Try replacing <? echo $missing; ?> with <?=$missing?>, and <? echo $complete; ?> with <?=$complete?>.

Also, you are never setting $complete to contain any value, and there are scenarios when it will not be defined at all, which will cause an error.

You should define it to be empty at the top of your PHP code (like you do with $missing), and then in your else statement assign some value to it, i.e. $complete = "Success!".

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

3 Comments

@LouysPatriceBessette I did not know that was an option, thanks for telling me about it!
@LouysPatriceBessette Edited to include that. You learn something new every day :D
1

See comments in code.

<?php 

$error = false;
$missing = "";

if ($_POST) {

  $complete = "<p><strong>Thank you!</strong> Your message was sent, we'll get back to you ASAP!</p>";

  if (filter_var($_POST["email"], FILTER_VALIDATE_EMAIL) === false ) {   // There was a missing parenthesis here.
    $missing .= "A vaild email address is required.<br>";
    $error = true;
  } 

  if ($_POST['subject']=="") {                       // Check if empty, not if exist... It may exist as empty.
    $missing .= "A subject field is required.<br>";
    $error = true;
  }

  if ($_POST['content']=="") {
    $missing .= "A content field is required.<br>";
    $error = true;
  }

  if ($error) {
    $missing = "<p>". $missing."</p>";
    $complete = "";                                   // Remove the success string here.
  }
} 

?>

Comments

1

I think it should not be (because when you submit the form it always post so you have to check whether it is blank or not)

if (!$_POST['subject'])    
&&  if(!$_POST['content'])
( -it means if data is not posted)

It should be

if($_POST['subject']=='')   
if($_POST['content']=='')'
( - it means if data is blank)

And in the HTML page you should use

<?php echo $missing; ?> or <?=$missing;?>
<?php echo $complete; ?> or <?=$complete;?>

Comments

0

Without seeing your form, it's hard to address anything there, but as you have it, it would be better to do a bit of a rework. You don't need the $error marker and you should store the errors in an array. If the array has a count greater than one at the end, then there is an obvious error. You should also remove empty spaces in the post values first before you determine if, in fact, the value is empty:

<?php 
# Make a function to remove spaces in the array. If the user types two empty
# spaces, it's considered not empty, so this will remove that and only leave
# actual string content (or empty).
function trimArray($array)
    {
        # Trim the value of empty spaces
        if(!is_array($array))
            # Send back since not an array
            return trim($array);
        # Loop through array and apply this same function
        foreach($array as $key => $value) {
            $array[$key] = trimArray($value);
        }
        # Return this array
        return $array;
    }

# Store your errors in an array
$missing = array();
# Check if the post is empty or not
if(!empty($_POST)){
    # Remove empty spaces from values in the post
    $POST = trimArray($_POST);
    # Check email
    if(!filter_var($POST["email"], FILTER_VALIDATE_EMAIL))
        $missing[] = "A vaild email address is required.";

    # Check if subject set
    if(empty($POST['subject']))
        $missing[] = "A subject field is required.";

    # Check if content
    if(empty($POST['content']))
        $missing[] = "A content field is required.";

    if(empty($missing)) {
        # Send mail here
        # Also check that it was successful...
    }
} 
?>

<!-- You only need one div that has a ternary condition. Set the class and message value -->
<!-- based on whether the $message array is empty or not -->
<form method="post" action="#">
    <h1>Get in touch!</h1>
    <?php
    # You have no neutral condition so if there is no post, then it will 
    # still come back as saying it's successful.
    # But that doesn't make any sense if no post was sent....
    if(isset($_POST['email'])) { ?>
    <div id="error" class="alert alert-<?php echo (!empty($missing))? 'danger' : 'success' ?>" role="alert">
        <p><?php echo (!empty($missing))? implode('<br />',$missing) : "<strong>Thank you!</strong> Your message was sent, we'll get back to you ASAP!" ?></p>
    </div>
    <?php } ?>

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.