Submitted PHP form and array response in particular location within HTML

I try to store result TRUE/FALSE when a form is submitted.
How to manage as message should be within particular location inside HTML template?

<?php

if ($_SERVER["REQUEST_METHOD"] == "POST") {
  $name = sanitize_text_field($_POST["name"]);
  $email = sanitize_email($_POST["email"]);
  $message = sanitize_textarea_field($_POST["message"]);
  
  if (mail($to, $subject, $message, $headers)) {
                // Display a success message
            echo '<div>Message is OK</div';
        } else {
            // Display an error message if the email sending failed
            echo '<div>Please try again!</div>';
        }

} else {
    echo "";
}

?>

Just as an example of response:

  if ($message_result === false) {
    $response = array(
      'success' => false,
      'message' => 'Error with a submitted message.',
    );
  } else {
    $response = array(
      'success' => true,
      'message' => '$pushMessageInPHPTemplate.'
    );
  }

How to push message using array inside particular location within HTML template?

I don’t understand your question, but please note that the return value from mail() is not an overall success or failure of the message. It states whether the mail was “accepted for delivery”, which means it was put in the local system’s outbound mail queue. That will be a success if mail is configured correctly, and has very little to do with the user’s perspective, which would be more like “did the mail get there”. And there are plenty of other ways the mail might still not get there. So suggesting the user “try again” is likely not useful. A message like “the mail system is not currently working - a message has been sent to the administrator” would be more appropriate.

1 Like

Please find an issue. Message should be shown only if sentence is TRUE as a logical statement.
I like to store HTML message into variable or an array like an example:

<?php

if ($_SERVER["REQUEST_METHOD"] == "POST") {
  $name = sanitize_text_field($_POST["name"]);
  $email = sanitize_email($_POST["email"]);
  $message = sanitize_textarea_field($_POST["message"]);
  
  if (mail($to, $subject, $message, $headers)) {
                // Display a success message
           $PUSH_MESSAGE_CONTAINER = ' <div</div>';
        } else {
            // Display an error message if the email sending failed
            echo '<div>Please try again!</div>';
        }

} else {
    echo "";
}

?>

and echo $PUSH_MESSAGE_CONTAINER variable into template position.

<!DOCTYPE HTML>

<html>

<head>
  <title>Push message in wordPress theme</title>
</head>

<body>
<?php echo $PUSH_MESSAGE_CONTAINER ?>
</body>

</html>

Need help how to execute inside a theme variable $PUSH_MESSAGE_CONTAINER only if ```
if (mail($to, $subject, $message, $headers))

is TRUE.

Are those two quoted pieces of code in the same file? They should be. If you set the variable $PUSH_MESSAGE_CONTAINER where you currently have it (you need to fix the broken html “<div” and then lower in the code in the same file you have whatever this template is, and echo out the same $PUSH_MESSAGE_CONTAINER, it will have whatever string you put into it. If the html of the template and your message all parse (ie, the net of it all needs to be valid html) then you should have what you need. That would be the normal way of processing a form and returning a success/failure message to the user.

The only thing I can see that would clearly be wrong is your various echoes on the first section of code, both the “Please try again” and the empty one, which need to also go below where you are outputting the $PUSH_MESSAGE_CONTAINER (i.e. store them as variables too and echo them in the right place in the template).

So you have one .php file, that looks like, in this order:

  1. Process the POST variables from the form
  2. If this was a POST, process error/success feedback to user
  3. otherwise If this was not POST, process initial form for collecting email

How to detect value as TRUE from such function?

$response = array(
  'success_message_container' => true,
 );

and if TRUE it will be pushed container

<?php

if ( is_success_message_container()) {
echo($PUSH_MESSAGE_CONTAINER);
}
?>

Can you write the whole code to validate as you put pure text.

It’s not clear why you need a function, but I’ll write two options. Here’s the code if you just store the true/false result in the $response array:

<?php
$PUSH_MESSAGE_CONTAINER = "Success!";

$response = array(
  'success_message_container' => true,
 );
 
if ($response['success_message_container']) {
	echo $PUSH_MESSAGE_CONTAINER;
}
?>   

Here’s the code if you need is_success_message_container() to be a function:

<?php
$PUSH_MESSAGE_CONTAINER = "Success!";

function is_success_message_container() :bool {
	return true;
}
 
if (is_success_message_container()) {
	echo $PUSH_MESSAGE_CONTAINER;
}
?> 

Thank you for the message!
Code pushes an error as attached image.
PHPError

Ah, sorry. You must be on an earlier version of PHP than I am. Remove “:bool” from that line.