My Contact Form works successfully and echos the “Sent Successfully” message after submitting.
How can I put the echoed message in a particular spot on the page, instead of it appearing at the very top of the page? For example, I’d like to have it appear just above the Form table. I Googled around, but couldn’t find the answer.
Here’s the php code:
mail($your_email, $subject, $message, $from);
$result = "Your Message Has Been Sent. Thank You";
echo "$result";
}
}
I can’t say from looking at that small amount of code.
But output is where the echo is.
So you could put the echo into the HTML wherever you want it.
What I prefer to do is instead of using echo in a function is to have the function return the value(s) and then echo the functions return value(s) in the HTML. I find it less trouble that way to debug and maintain.
Presuming you have the php in with the html, as the start, that’s why the message is echoed at the top.
Just move the echo to where you want it in the html.
To me it would make sense to change the $error variable to $result, that way the echoed result would be the result of the form either way.
$result = "Please Complete All Required* Fields";
The result get echoed wherever you want it in the html.
In this example, the end.
This may not be the reason why the message is not displaying, but that html above is totally invalid.
Put the <p> before the <table> and if you must format the form with a table, wrap the <form> around the whole <table>.
I have error reporting on - I see no errors anywhere.
I’m not sure what to do with this information:
“page loads with $message as an empty string
form is submitted, $message may be assigned a non-empty string
page loads again from “scratch” so $message is again an empty string”
The html for the form result is basically whatever HTML you output from the mail-sending code (in your case I think it’s "../index.php" because that’s your form destination. When your user presses the submit button, the form data is sent to that PHP code, and what appears on the page from that point on is whatever that php script outputs. So are you saying that when the mail is sent, you just get a blank screen with your success message on it? Or do you get something else? If the latter, then that html code is where your message should be added.
Obviously another way is to use Ajax to process the form, so that the display remains in place. That would allow you to do something like replace the div containing the form with a “Please wait, sending…” message, and then replace it again on success with your “Message sent” message.
I earlier assumed that the php processing code and the html for the form, were both within the same document.
But seeing your form action:-
action="../index.php"
This s evidently not the case, so the result should not be echoed in the form page. I should be in whatever html document you are displaying after from submission, which you have yet to show us.