Jump to content

PHP coding for contact form


Recommended Posts

Hi, I’ve been trying to get a functioning contact form for a website that I’m building and, thus far, I haven’t been able to accomplish this. I’ve worked on this for a few days now and read through the tutorials but I still don’t under what I’m doing. Probably because I struggle to understanding coding. The code that I’m working with is included below. Thank you in advance for any help you can give me!


Here is the code that I have placed in the head of the document:

<link rel="action_page" href="action_page.php">


action_page.php code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">


<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

<title>Untitled Document</title>



// define variables and set to empty values

$nameErr = $emailErr = $subjectErr = $messageErr = "";

$name = $email = $subject = $message = "";


if (empty($_POST["name"])) {

$nameErr = "Name is required";

} else {

$name = test_input($_POST["name"]);

// check if name only contains letters and whitespace

if (!preg_match("/^[a-zA-Z-' ]*$/",$name)) {

$nameErr = "Only letters and white space allowed";



if (empty($_POST["email"])) {

$emailErr = "Email is required";

} else {

$email = test_input($_POST["email"]);

// check if e-mail address is well-formed

if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {

$emailErr = "Invalid email format";




if (empty($_POST["subject"])) {

$subjectErr = "Subject is required";

} else {

$subject = test_input($_POST["subject"]);




if (empty($_POST["message"])) {

$message = "Message is required";

} else {

$message = test_input($_POST["message"]);





Here is the html code for the form itself:


<form action="action_page.php" method="post" target="_blank">

<p><input class="w3-input w3-border" type="text" placeholder="Name" required name="Name"></p>

<p><input class="w3-input w3-border" type="text" placeholder="Email" required name="Email"></p>

<p><input class="w3-input w3-border" type="text" placeholder="Subject" required name="Subject"></p>

<p><input class="w3-input w3-border" type="text" placeholder="Message" required name="Message"></p>

<p><button class="w3-button w3-black" type="submit">

<i class="fa fa-paper-plane"></i> SEND MESSAGE</button>


Link to comment
Share on other sites

Hi, thank you very much for your response!

When I test the contact form, I get basically the same error message in all browsers and I've included the one from Firefox ... 


File not found

Firefox can't find the file at /action_page.php?

Is it because I'm testing this on my laptop? The files are not on a server yet. Also, where would I include the email address to route this to? For sure, I'm not doing this correctly. By the way, I removed the link element from the page.

Thank you again for your help!

Link to comment
Share on other sites

  • 2 weeks later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Create New...