Jump to content

Recommended Posts

I made a website using HTML and some javascript. I tried to make a contact form with PHP, but something goes wrong when submitting and I don't understand why.
I followed a tutorial and can't see any mistakes (first time using PHP, so there might be a lot I can't see).

Here is the HTML-code and PHP-file. I also uploaded the original, in case you prefer to open it that way..

<!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>

    <link rel="stylesheet" type="text/css" href="sample.css">
	<style type="text/css" media="all">
		@import "sample.css";
	</style>
    
<title>Yan Zhou | CONTACT</title>
</head>

<body>
<div id="container">
<div id="header_home"></div>
        <div id="navigatie">
        <ul>
            <li id="linkabout"><a href="Yan Zhou - About.html"><img src="images/link_about.png" alt="ABOUT"></a></li>
            <li id="linkportfolio"><a href="Yan Zhou - Portfolio.html"><img src="images/link_portfolio.png" alt="PORTFOLIO"></a></li>
            <li id="linkcontact"><a href="Yan Zhou - Contact.html"><img src="images/link_contact.png" alt="CONTACT"></a></li>
        </ul>
        </div>
<div id="supportingText">
   <form action="contact_form.php">

    <label for="name">Name</label>
    <input type="text" id="name" name="name" placeholder="I will need your name for contacting you. ^^">

    <label for="company">Company</label>
    <input type="text" id="company" name="company" placeholder="If you contact me on behalf of a company, type it here. Otherwise type 'None'.">
    
    <label for="email">Email</label>
    <input type="text" id="email" name="email" placeholder="How else could I answer you?">

    <label for="message">Message</label>
    <textarea id="message" name="message" placeholder="Who are you, where are you from, what do you wish to say? :-)" style="height:200px">
    </textarea>

    <input type="submit" value="Submit">

  </form>
 </div>



</body></html>
<?php
if(isset($_POST['email'])) {
 
    // EDIT THE 2 LINES BELOW AS REQUIRED
    $email_to = "rakkermans@hotmail.com";
    $email_subject = "Yan Zhou Website - Contactformulier";
 
    function died($error) {
        // your error code can go here
        echo "I'm sorry, but there were error(s) found with the form you submitted. ";
        echo $error."<br /><br />";
        echo "Please fix these errors and retry submitting.<br /><br />";
        die();
    }
 
 
    // validation expected data exists
    if(!isset($_POST['name']) ||
        !isset($_POST['company']) ||
        !isset($_POST['email']) ||
        !isset($_POST['message'])) {
        died('We are sorry, but there appears to be a problem with the form you submitted.');       
    }
 
     
 
    $name = $_POST['name']; // required
    $company = $_POST['company']; // 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(!preg_match($string_exp,$company)) {
    $error_message .= 'The Company you entered does not appear to be valid.<br />';
  }
 
  if(strlen($message) < 2) {
    $error_message .= 'The message you entered does not appear to be valid.<br />';
  }
 
  if(strlen($error_message) > 0) {
    died($error_message);
  }
 
    $email = "Form details below.\n\n";
 
     
    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 .= "Company: ".clean_string($company)."\n";
    $email_message .= "Email: ".clean_string($email)."\n";
    $email_message .= "Message: ".clean_string($message)."\n";
 
?>
 

<?php ?>

The following error message appears

Quote


"; echo "Please fix these errors and retry submitting.

"; die(); } // validation expected data exists if(!isset($_POST['name']) || !isset($_POST['company']) || !isset($_POST['email']) || !isset($_POST['message'])) { died('We are sorry, but there appears to be a problem with the form you submitted.'); } $name = $_POST['name']; // required $company = $_POST['company']; // 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.
'; } $string_exp = "/^[A-Za-z .'-]+$/"; if(!preg_match($string_exp,$name)) { $error_message .= 'The Name you entered does not appear to be valid.
'; } if(!preg_match($string_exp,$company)) { $error_message .= 'The Company you entered does not appear to be valid.
'; } if(strlen($message) < 2) { $error_message .= 'The message you entered does not appear to be valid.
'; } if(strlen($error_message) > 0) { died($error_message); } $email = "Form details below.\n\n"; 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 .= "Company: ".clean_string($company)."\n"; $email_message .= "Email: ".clean_string($email)."\n"; $email_message .= "Message: ".clean_string($message)."\n"; ?>

Thanks for looking into it!

 

Rob

Yan Zhou - Contact.html

contact_form.php

Link to post
Share on other sites

If you want to test locally, you can download a program like XAMPP that includes a web server, PHP, MySQL, etc and just have a web server running on your local machine where you can test your code.

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...