I’ve set up a contact form for a website I’m making.
The pages involved is contact.html which is the page the HTML form is on, and mailer.php which contains the PHP script that sends an email.
The form works fine, but I really want to add in a script of sorts that sets every input box (Name, Email and Enquiry in this case) as a required field, and doesn’t send the email unless each field has something type in it.
I’ve tried a couple of Javascript scripts I found online, but they are not working…
Can anyone give me a 100% guaranteed working script? I don’t care if it’s Javascript or PHP, whatever works.
I would use javascript for this. It won’t be 100%, because a very small percentage of people have javascript turned off, but it works well enough for me.
Then, in the head of the page, create a javascript file to check the form fields:
<script type="text/javascript">
function verify()
{
var form = document.mailer;
var error = '';
if(form.name.value == '') { error += "Please enter your name before pressing submit
"; }
if(form.email.value == '') { error += "Please enter your email before pressing submit
"; }
if(form.enquiry.value == '') { error += "Please enter your enquiry before pressing submit
"; }
if(error != '') { alert(error); return false; }
else { return true; }
}
</script>
This should work. If you really want to make sure that you are not getting garbage, you can check the email on the server side after it is submitted to make sure it’s valid using regular expressions