ren Posted October 25, 2021 Share Posted October 25, 2021 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"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>Untitled Document</title></head><body> <?php// define variables and set to empty values$nameErr = $emailErr = $subjectErr = $messageErr = "";$name = $email = $subject = $message = ""; if ($_SERVER["REQUEST_METHOD"] == "POST") { 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"]); }?></body></html> 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> </p> </form> Link to comment Share on other sites More sharing options...
dsonesuk Posted October 26, 2021 Share Posted October 26, 2021 The link element is used to link to a stylesheet and i can't remember using it for anything else to be honest. The form 'action' tells it where to go after form submission. Link to comment Share on other sites More sharing options...
ren Posted October 26, 2021 Author Share Posted October 26, 2021 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 ... Quote 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 More sharing options...
dsonesuk Posted October 26, 2021 Share Posted October 26, 2021 You are missing the vital part of sending the email using php mail() at https://www.w3schools.com/php/func_mail_mail.asp Quote Is it because I'm testing this on my laptop? The files are not on a server yet. ??? You need a localhost server to process php , the validation and mail() won't work without it! Link to comment Share on other sites More sharing options...
ren Posted October 26, 2021 Author Share Posted October 26, 2021 Okay, thank you for your help with this! I knew I was doing something wrong. I appreciate the guidance very much. Thanks! Quote Link to comment Share on other sites More sharing options...
Hasan Ali Posted October 30, 2021 Share Posted October 30, 2021 I have checked your code and I think you need to study about PHP mail function. You can go here - https://www.w3schools.com/php/func_mail_mail.asp Link to comment Share on other sites More sharing options...
ren Posted November 10, 2021 Author Share Posted November 10, 2021 Hi Hassan, thank you very much for your response. I’m still trying to figure this out. Thanks again! Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now