Send button not sending – PHP – SitePoint Forums


Hi people.
This is my first attempt at creating a form. Unfortunately I’m having trouble getting it to work.
The form itself works fine, but the submission part does not.
I tried to follow other forms from other forums as best as I can, but I’m missing something.
I struggled with:

Here I have separated the forms.
I had

I added and removed PHP tags. I also played with them by moving them to several places.

The code I have at the moment is:


You can use this form to contact us

Sorry if I make any newbie mistakes.
If anyone can point me in the right direction.

You should only have one form tag.

Is the code for sending the email in a separate PHP file and named appropriately? You quote the action as “contactform” – shouldn’t that be “contactform.php”?

As for actually sending the email, many commercial email servers won’t just send email “from” the address your user has entered in the forum. A proper mail server will be configured to only send email from a domain it’s supposed to send from, otherwise it’s a thing called “Open Relay” which is a bad thing. Use an email address that you’ve configured the server for and use the “reply-to” header if you want to make it easier to reply.

I should also mention that the built-in PHP mail() is not considered a good way to send emails, you should check out a library such as PHPMailer. This may be especially necessary if you are using gmail as your mail server, as this will require a secure connection on another port. Is your development system properly configured to support sending email?

As mentioned, your form element(s) are wrong. Have a single form element with all the attributes it contains.

Other than that, there are things that could be done differently.
You cannot rely on client-side validation alone, it must also be backed up by server-side validation.
I would probably remove the js validation, because basics like this are now built into browsers if you use the correct attributes in the inputs, such as the required keyword. This will let you focus on server-side validation.

It seems that the PHP form is processed with the HTML of the page. It’s doable, but if you do it, do it differently. Put the PHP at the beginning of the file, before any HTML and wrap the processing in a condition, to only trigger on submit:-

This way you don’t even need the action attribute, because it will default to itself.
It also means that if a submission fails server-side validation, you can send the user back to the form and list the errors they made.
A successful submission can be redirected to a success page via headers.


Comments are closed.