Hallo, i have problem with my form. Before sending, form is validating but despite the errors, mail is sending. I would like to send mail after correct validation. I have 3 rquired and 2 optional fields.
There is a missing a quote in the subject variable.
As is, the script will attempt to run as soon as it is called. Since the form is not posted I assume it is a separate page. Better to have a single page. Nevertheless, you should still check the REQUEST METHOD before the code runs.
if($_SERVER[âREQUEST_METHODâ] == âPOSTâ){
//Process Form
}
Next, all the implodes. Are those fields really arrays? If not, get rid of it.
Next, all the elseâs and variables for nothing should go. You already have the POST variables, just use them, unless something is changed (i.e the implode is actually valid or some other transformation) The logic should be to just validate and populate the error array if there are any errors. No elseâs.
The count in the errors check is not needed. This
if (count($errors)) {
Could simply be
if ($errors) {
The empty $body variable is completely pointless. And really, one $body is enough. You donât need to break it into five.
There is also the issue of an Injection Attack which I wont get into except to say, NEVER trust user input.
The isset check here is incorrect. If the user is using your form and the field is written correctly, it will always be isset. You need to check if empty.
if (!isset($_POST[âprofilâ]))
As is, the form will validate if the user submits spaces. You need to trim the POST array before the empty checks which will also eliminate the need for the null coalesce operator (Double question marks ??)