I set up a form mail page on my website, using if … else and the isset function.
However, mail is always sent on ‘submit’ regardless of whether the fields in the form have been completed, and thankyou.htm is always returned.
Using client side validation like this is helpful and I would recommend it. But it can’t be relied upon, it should be backed up by server side validation too.
Add to that making the input type for email, email, modern browsers will validate client side if you do.
As mentioned, this won’t work because if they are not filled in, they will still be set, just set as an empty string.
Use the condition @Gandalf posted to confirm form submission, then run the mail script.
Thank you! I must say I’m a bit perplexed by ‘preferred’. I was under the impression it either worked or didn’t, and I couldn’t find any errors in my script. But I will definitely experiment with the script sample you offered and report back.
The purpose of the initial conditional statement of the post method form processing code is to detect when the form has been submitted, so that the processing code will only run if there is data to operate on. That’s the only thing your line of code is doing, to test if 3 of the 4 (presumably ‘subject’ got left out due to the tedium of typing all of that) always-set form fields are set (repeating now, but most form field types will always be set when the form has been submitted, even if they are empty.)
Programming is already a tedious typing activity. If you find yourself repeating elements, lines, and blocks of code that only differ in the variable or value they operate on or have a series of name-numbered things, it is a sign that you are doing something in the hardest way possible. If your form had 30 fields, would writing out 30 entries in the isset() statement or having 30 lines of code copying variables to other variables make sense? Your answer should be no.
The statement testing if the server request method is equal to post, accomplishes testing if a post method form was submitted. This is simple, general-purpose, and reusable.
As to copying a bunch of variables to other variables. If you are not modifying the values at the same time, and are doing this to preserve the original values, it is a waste of time typing. As already mentioned, you should trim all input data before using it, mainly so that you can detect if all white-space characters were entered. If you keep the form data as a set, in an array variable, you can trim it all at once using one single line of code, storing the trimmed working copy in an internal array variable. You would operate on elements of this internal array variable throughout the rest of the script. In addition to savings typing a bunch of lines of code, by keeping the form data as a set, you are now in a position where you can dynamically validate the data, dynamically process the data, and can simply provide an array of data to a html document template to output the data on a web page.
Thank you for taking the time to elaborate. I’m very new to this and a form mail is the first ‘real’ project that I am aiming to set up. Unfortunately, I have seen very few (appropriately simple) examples to work from, so finding this forum has been a great discovery because I am able to benefit from the generosity of you and people like you. Hopefully in time I’ll be able to help you too.
The other problem you’ll undoubtedly run in to as you search is that of old out-of-date tutorials that show you how to do stuff in old, out-of-date ways. This is particularly true in database handling - there are many, many tutorials out there that use the old-style mysql_query() and similar functions, which were deprecated for many years, and are now removed from the language. Similarly lots that don’t talk about using Prepared Statements for database handling. A useful skill to develop will be to distinguish a good tutorial from an outdated one.
It’s also only a matter of time before someone recommends that you don’t use the built-in PHP mail() function, and look at using a library such as “PHPMailer” instead, as it’s more reliable and supports more functionality.