I’m having a weird error that I cannot figure out with a PHP file, I’ve never touched PHP and I am trying to use it to send a Name, Email Address, and a message back to me from a website, but for some reason, the code I wrote is just downloading the PHP file instead, can anyone else see where I may be going wrong here:
HTML
<div class="modal-body"> <div class="container"> <!-- TODO add in contact form --> <form action="form-to-action.php"> <label for="fname">Name</label> <input type="text" id="Name" name="Name" placeholder="Name"> <label for="lname">Email Address</label> <input type="text" id="Email" name="Email" placeholder="Email Address"> <br> <label for="subject">How can we Help?</label> <textarea id="Message" name="Message" placeholder="Write something.." style="height:200px"></textarea> <input type="submit" value="Send"id="contactBtnModal"style="text-decoration: none;" > </form> </div> </div>
PHP:
<?php if (isset($_POST['Email'])) { // EDIT THE 2 LINES BELOW AS REQUIRED $email_to = "testingemail@gmail.com"; $email_subject = "Testing"; function problem($error) { echo "We are very sorry, but there were error(s) found with the form you submitted. "; echo "These errors appear below.<br><br>"; echo $error . "<br><br>"; echo "Please go back and fix these errors.<br><br>"; die(); } // validation expected data exists if ( !isset($_POST['Name']) || !isset($_POST['Email']) || !isset($_POST['Message']) ) { problem('We are sorry, but there appears to be a problem with the form you submitted.'); } $name = $_POST['Name']; // required $email = $_POST['Email']; // required $message = $_POST['Message']; // required $error_message = ""; $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+.[A-Za-z]{2,4}$/'; if (!preg_match($email_exp, $email)) { $error_message .= 'The Email address you entered does not appear to be valid.<br>'; } $string_exp = "/^[A-Za-z .'-]+$/"; if (!preg_match($string_exp, $name)) { $error_message .= 'The Name you entered does not appear to be valid.<br>'; } if (strlen($message) < 2) { $error_message .= 'The Message you entered do not appear to be valid.<br>'; } if (strlen($error_message) > 0) { problem($error_message); } $email_message = "Form details below.nn"; function clean_string($string) { $bad = array("content-type", "bcc:", "to:", "cc:", "href"); return str_replace($bad, "", $string); } $email_message .= "Name: " . clean_string($name) . "n"; $email_message .= "Email: " . clean_string($email) . "n"; $email_message .= "Message: " . clean_string($message) . "n"; // create email headers $headers = 'From: ' . $email . "rn" . 'Reply-To: ' . $email . "rn" . 'X-Mailer: PHP/' . phpversion(); @mail($email_to, $email_subject, $email_message, $headers); ?> <!-- include your success message below --> Thank you for contacting us. We will be in touch with you very soon. <?php } ?>
Advertisement
Answer
Are you installed a PHP server in your Computer?
If not, download WAMP and try again, you need access the page via http://localhost, and not via file://path_to_document in your web navigator.
Or run the script in a web server.