1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

How to submit PHP contact form to an email

Discussion in 'PHP' started by imza86, May 18, 2016.

  1. #1
    Hi, so i have done a few tutorials in making a contact form. But the important part is missing, how do i submit it to an email ? The php form code i made is as below:

    <!DOCTYPE HTML>
    <html>
    <head>
    <style>
    .error {color: #FF0000;}
    </style>
    </head>
    <body>

    <?php
    // define variables and set to empty values
    $nameErr = $emailErr = $genderErr = $websiteErr = "";
    $name = $email = $gender = $comment = $website = "";

    if ($_SERVER["REQUEST_METHOD"] == "POST") {
    if (empty($_POST["name"])) {
    $nameErr = "Name is required";
    } else {
    $name = $_REQUEST['name'];
    if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
    $nameErr = "Only letters and white space allowed";
    }
    }
    if (empty($_POST["email"])) {
    $emailErr = "Email is required";
    } else {
    $email = $_REQUEST['email'];
    if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
    $emailErr = "Invalid email format";
    }
    }

    if (empty($_POST["website"])) {
    $website = "";
    } else {
    $website = $_REQUEST['website'];
    }

    if (empty($_POST["comment"])) {
    $comment = "";
    } else {
    $comment = $_REQUEST['comment'];
    }

    if (empty($_POST["gender"])) {
    $genderErr = "Gender is required";
    } else {
    $gender = $_REQUEST['gender'];
    }
    }

    ?>

    <h2>PHP Form Validation Example</h2>
    <p><span class="error">* required field must fill in!.</span></p>
    <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
    Name: <input type="text" name="name">
    <span class="error">* <?php echo $nameErr;?></span>
    <br><br>
    E-mail: <input type="text" name="email">
    <span class="error">* <?php echo $emailErr;?></span>
    <br><br>
    Website: <input type="text" name="website">
    <span class="error"><?php echo $websiteErr;?></span>
    <br><br>
    Comment: <textarea name="comment" rows="5" cols="40"></textarea>
    <br><br>
    Gender:
    <input type="radio" name="gender" value="female">Female
    <input type="radio" name="gender" value="male">Male
    <span class="error">* <?php echo $genderErr;?></span>
    <br><br>
    <input type="submit" name="submit" value="Submit">
    </form>

    <?php
    echo "<h2>Your Input:</h2>";
    echo "Name:$name";
    echo "<br>";
    echo "Email:$email";
    echo "<br>";
    echo "Website: $website";
    echo "<br>";
    echo "Comment:$comment";
    echo "<br>";
    echo "Gender: $gender";
    ?>

    </body>
    </html>
     
    imza86, May 18, 2016 IP
  2. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #2
    That contact-page is... rather garbage, here's some updated code, which include a way to send email (however, that sending code isn't secure) - it's a starting point:
    
    <?php
    echo '<!DOCTYPE html>
    <html>
       <head>
         <title>Contact form</title>
         <style>
           .error,.required {color: #FF0000;}
           form {
             width: 30em;
           }
           label {
             font-weight: bold;
             margin-top: 1em;
             display: inline-block;
             width: 8em;
           }
           input, textarea {
             margin-top: 1em;
             min-width: 60%;
           }
           input[type=submit] {
             margin: 1em auto;
             display: block;
           }
         </style>
       </head>
    <body>';
    
    $name = (isset($_POST['name'])) ? $_POST['name'] : '';
    $email = (isset($_POST['email'])) ? $_POST['email'] : '';
    $website = (isset($_POST['website'])) ? $_POST['website'] : '';
    $comment = (isset($_POST['comment'])) ? $_POST['comment'] : '';
    $gender = (isset($_POST['gender'])) ? $_POST['gender'] : '';
    
    $nameErr = (isset($_POST['submit']) && empty($_POST['name']) ? 'Name is required' : (!preg_match('/^[a-zA-Z ]*$/',$_POST['name']) ? 'Only letters and whitespace allowed' : ''));
    $emailErr = (isset($_POST['submit']) && empty($_POST['email']) ? 'Email is required' : (isset($_POST['submit']) && !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) ? 'Invalid email format' : ''));
    $genderErr = (isset($_POST['submit']) && empty($_POST['gender']) ? 'Gender is required' : '');
    
    echo '
    
       <h2>PHP Form Validation Example</h2>
       <p class="required">* required field must filled in!</p>
       <form method="post">
         <label for="name">Name: <span class="required">*</span></label>
           <input type="text" id="name" name="name" required><br>';
           if (!empty($nameErr)) {
             echo '<span class="error">'.$nameErr.'</span><br>';
           }
    echo '   <label for="email">E-mail: <span class="required">*</span></label>
         <input type="email" id="email" name="email" required><br>';
         if (!empty($emailErr)) {
             echo  '<span class="error">'.$emailErr.'</span><br>';
         }
    echo '   <label for="website">Website: </label>
         <input type="text" id="website" name="website"><br>
         <label for="comment">Comment: </label>
         <textarea id="comment" name="comment"></textarea><br>
         <label>Gender: <span class="required">*</span></label><br>
         <label for="female">Female: </label><input type="radio" id="female" name="gender" value="female"><br>
         <label for="male">Male: </label><input type="radio" id="male" name="gender" value="male">';
         if (!empty($genderErr)) {
           echo '<span class="error">'.$genderErr.'</span><br>';
         }
    echo '<input type="submit" name="submit" value="Submit">
    </form>';
    
    if (isset($_POST['submit']) && empty($nameErr) && empty($emailErr) && empty($genderErr)) {
       echo '<h3>Your Input:</h3>
         <ul>
           <li>Name: '.$name.'</li>
           <li>Email: '.$email.'</li>
           <li>Website: '.$website.'</li>
           <li>Comment: '.$comment.'</li>
           <li>Gender: '.$gender.'</li>
         </ul>';
    
       $email_recipient = 'insert an email to send it to here';
    
       $subject = 'This was posted';
    
       $message = 'Sender: '.$name.' '.$email;
       $message .= 'Website: '.$website;
       $message .= 'Comment: '.$comment;
    
       send_mail($email_recipient,$subject,$message,'From: '.$email.'');
    }
    
    
    echo '</body>
    </html>';
    
    ?>
    
    PHP:
     
    PoPSiCLe, May 18, 2016 IP