I checked so many posts on here but I am unable to fix this.
I have a PHP form and although the email is sent it doesn’t show any massage and I don’t know what I am missing.
In the PHP it mentions a success and failed but no reference on where that message sits? I set up PHP forms before but this is throwing me off.
Ok so you are passing the message back to contact.html and that is where you are saying it is not showing the message, on the page right?
Assuming this is what you are talking about, it is because you don’t have anything on contact.html that reads that passed message, evaluates it and displays it on page.
Now I would suggest that unless you configured the server to pass .html files to PHP, you probably want to make sure this is contact.php and not contact.html. Then in the body of the page you are going to want to put in some conditional logic to look for a message being passed to the page. Based on that message you can then display any error/success message.
<div class="message-response">
<?php
if (isset($_GET['message'])) { //<--- Read the message passed to page
if ( $_GET['message'] === 'Successfull') { //<-- Was it successful? If so, display success
echo "Hurray, message sent successfully!";
} else {
echo "Uh oh, something went wrong."; // <-- Error? Say so.
}
}
?>
</div>
<div class="inner wow fadeInLeft" data-wow-delay="0ms" data-wow-duration="1500ms">
<div class="sec-title">
<h2>Send <strong>Your Message</strong></h2>
<div class="lower-text">Don’t Hesitate to Contact Us</div>
</div>
Hopefully this is what you are talking about. Otherwise please clarify a bit further on what is exactly not happening that you think it shou.d
Thank you for your answer. The issue is related that a client had his website done from a template and now asking me to edit some bits.
So, I have created a contact.php, pasted the code you wrote above and changed the code in sendmail.php to the following but it’s still not working:
Also, the example given to you for contact.php uses $_GET but you used $_POST. You don’t use the
syntax with POST.
I am no expert in this but wonder if this is the issue?
(also, you have spelt successful incorrectly but as it is only being used as a string and the same incorrect spelling is being tested for then that shouldn’t be a problem!)
Actually I would strongly suggest not to do this. It is often much easier and better to go back to the same page as the form and just display the error messages than breaking it out into other pages. Anyone who has done much bigger projects will tell you, having separate pages just to show messages like this is really tedious and time consuming.
@johnbiondini The code you added there with setting the $success variable doesn’t really address the issue of displaying based on that variable. Look back at my example. The code I am showing is back in your HTML form code. That is the page that gets passed the “message” value as part of the URL. Are you able to alter that template at all for the client or is that something that is fixed in place?
@Martyr2
If the form submission is successful there is usually no point in continuing to display the form. Is it not true that most websites redirect to a separate success page? I have a client website with about 10 forms and just one success page. That’s not tedious or time-consuming.
In this case the form already has HTML5 validation so the chance of failure is small. A failure web page could have a ‘Back’ button to navigate back to the form. I accept that it is sometimes necessary to have more sophisticated client-side JavaScript validation and/or server-side PHP validation. Such validation should highlight the input fields that are failing validation and provide appropriate messages. Your code is not doing that.
If the form is successful, sure you can go to another page. When submitting the form however, you typically submit it to itself in the case of the opposite outcome, it is not successful in which case you would show the error message. Now having said that, it also depends. In many cases the form may show a successful and still redirect back to the form. Think of a form that “saves”. Sometimes you redirect back with a success message only to show the form again for future edits.
Now if you were referring simply that they could just redirect if the outcome was successful, then I agree. I was just saying that in most cases you submit to itself and if it is successful, then do something. Otherwise you want to go back to the form again with an error message so the user can try again. If I misunderstood your statement, then I apologize. But I do stand by the idea that you typically would submit back to itself because a success doesn’t always mean a separate page. I was also under the impression you were also saying a separate page for failure as well.
But in any case, the typical pattern is you submit the form back to its page, if successful do something and if not, show the message and display the form again.
External data can be anything, can come from anywhere, and cannot be trusted. You MUST validate data on the server before using it, and then you must use it in a safe way appropriate for the context.
Client-side validation is just a nicety for legitimate visitors.
If you fill up the details and click “Send” the email will be delivered but the user just sees the page refreshed and no indication on whether the email was successfully sent or not.
I can see several issues, most of which have been mentioned above.
Your form uses the method PUT but you are using the syntax for GET (using “?”)
Your form action is “sendemail.php” but your page is called “sendmail.php” (although if that were the case then I would not expect the email to send at all)
Your contact page suffix is .html - it should be .php or otherwise the PHP will not run.