Skip to main content
Known Participant
June 10, 2017
Answered

Need help with PHP Contact Form

  • June 10, 2017
  • 1 reply
  • 828 views

Hello,

For the last day or so i've been really struggling with correctly making a PHP contact form!  If somebody can point me towards a great tutorial to do so or just help me in anyways that would be great.  Im just looking for a basic form that will request Name, Email, and Phone number.  Thankyou!

    This topic has been closed for replies.
    Correct answer Nancy OShea

    Ok just 1 more thing, Sorry! 

    a.) i have html form done,

    b.) all the php from part 2 is what i need at the top of the php form?

    c.) part 3 is the php i put into html file?


    When done, your contact.php document should look like this.  Change valid address and email as required.

    <?php

    // NOTE: this page must be saved as a .php file.

    // And your server must support PHP 5.3+ and phpMail().

    // Define variables and set to empty values

    $result = $name = $email = $phone = $human = "";

    $errName = $errEmail = $errPhone = $errHuman = "";

        if (isset($_POST["submit"])) {

            $name = $_POST['name'];

            $email = $_POST['email'];

            $phone = $_POST['phone'];

            $human = intval($_POST['human']);

        //valid address on your web server

            $from = 'webmaster@yourdomain.com';

        //your email address where you wish to recieve mail

            $to = 'you@yourdomain.com';

            $subject = 'MESSAGE FROM YOUR WEB SITE';

            $headers = "From:$from\r\n Reply-to:$email";

            $body = "From: $name\n E-Mail: $email\n Phone: $phone";

    // Check if name is entered

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

    $errName = "Please enter your name.";

    } else {

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

    }

    // Check if email is entered

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

    $errEmail = "Please enter your email address.";

    } else {

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

        // check if e-mail address is valid format

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

          $errEmail = "Invalid email format.";

        }

    }

    // Check if phone is entered

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

    $phone = "";

    } else {

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

    }

    //Check if simple anti-bot test is entered

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

    $errHuman = "Please enter the sum.";

    } else {

    //12 = the sum of 6 + 6, if you change the test question, make sure to change this answer.

         if ($human !== 12) {

         $errHuman = 'Wrong answer. Please try again.';

            }

    }

    // If there are no errors, send the email & output results to the form

    if (!$errName && !$errEmail && !$errPhone &&  !$errHuman) {

        if (mail ($to, $subject, $body, $from)) {

            $result='<div class="alert alert-success"><h2><span class="glyphicon glyphicon-ok"></span> Message sent!</h2><h3>Thank you for contacting us. Someone will be in touch with you soon.</h3></div>';

        } else {

            $result='<div class="alert alert-danger"><h2><span class="glyphicon glyphicon-warning-sign"></span> Sorry there was a form processing error.</h2> <h3>Please try again later.</h3></div>';

           }

        }

    }

        //sanitize data inputs   

        function test_input($data) {

       $data = trim($data);

       $data = stripslashes($data);

       $data = htmlspecialchars($data);

        $data = (filter_var($data, FILTER_SANITIZE_STRING));

       return $data;

    }

    //end form processing script

    ?>

    <!doctype html>

    <html lang="en">

    <head>

    <meta charset="utf-8">

    <title>Bootstrap and PHP Script</title>

    <meta name="viewport" content="width=device-width, initial-scale=1">

    <meta http-equiv="X-UA-Compatible" content="IE=edge">

    <!--[if lt IE 9]>

    <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>

    <![endif]-->

    <!--Bootstrap-->

    <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">

    <style>

    .required {color:red; font-weight:bold}

    .center-block {float:none}

    .human {margin: 0 0 0 12px}

    </style>

    </head>

    <body>

    <div class="container">

    <div class="row">

    <div class="col-lg-8 col-lg-offset-2">

    <div class="col-md-8 center-block">

    <h3>Responsive Contact Form</h3>

    <p class="required small">* = Required fields</p>

    <!--begin HTML Form-->

    <form class="form-horizontal" method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">

    <!--when submit button is clicked, show results here-->

    <div class="form-group">

    <div class="col-sm-10 col-sm-offset-2">

    <?php echo $result;?>

    </div>

    </div>

    <div class="form-group">

    <label for="name" class="col-sm-3 control-label"><span class="required">*</span> Name:</label>

    <div class="col-sm-9">

    <input type="text" class="form-control" id="name" name="name" placeholder="First & Last" value="<?php echo $name; ?>">

    <span class="required small"><?php echo $errName; ?></span>

    </div>

    </div>

    <div class="form-group">

    <label for="email" class="col-sm-3 control-label"><span class="required">*</span> Email: </label>

    <div class="col-sm-9">

    <input type="email" class="form-control" id="email" name="email" placeholder="you@domain.com" value="<?php echo $email; ?>">

    <span class="required small"><?php echo $errEmail;?></span>

    </div>

    </div>

    <div class="form-group">

    <label for="phone" class="col-sm-3 control-label">Phone: </label>

    <div class="col-sm-9">

    <input type="tel" class="form-control" id="phone" name="phone" placeholder="(123) 456-7890" value="<?php echo $phone; ?>">

    <span class="required small"><?php echo $errPhone;?></span>

    </div>

    </div>

    <div class="form-group">

    <label for="human" class="col-sm-3 control-label"><span class="required">*</span> Human Test:</label>

    <div class="col-sm-4">

    <h3 class="human">6 + 6 = ?</h3>

    <input type="text" class="form-control" id="human" name="human" placeholder="Your Answer" value="<?php echo $human; ?>">

    <span class="required small"><?php echo $errHuman;?></span>

    </div>

    </div>

    <div class="form-group">

    <div class="col-sm-offset-3 col-sm-6 col-sm-offset-3">

    <button type="submit" id="submit" name="submit" class="btn-lg btn-primary btn-block">SUBMIT</button>

    </div>

    </div>

    <!--end Form--></form>

    <!--end col block--></div>

    <!--end col--></div>

    <!--end row--></div>

    <!--end container--></div>

    <!--latest minified & compiled jQuery-->

    <script src="https://code.jquery.com/jquery-1.12.2.min.js" integrity="sha256-lZFHibXzMHo3GGeehn1hudTAP3Sc0uKXBXAzHX1sjtk=" crossorigin="anonymous"></script>

    <!--Latest minified & compiled Bootstrap-->

    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>

    </body>

    </html>

    1 reply

    Nancy OShea
    Community Expert
    Community Expert
    June 10, 2017
    Nancy O'Shea— Product User & Community Expert
    CheddaaAuthor
    Known Participant
    June 10, 2017

    Thanks again Nancy, Still having trouble with the PHP part seeing as i literally have zero clue about it!  Im just stuck at work staring at my computer screen..

    Nancy OShea
    Community Expert
    Community Expert
    June 10, 2017

    PHP coding is not too different from JavaScript coding.

    I'm afraid nobody here can teach you PHP.  That goes beyond the scope of a public forum.  Go to Lynda.com and take David Powers' course on Learning PHP.   He breaks his lessons into many short video segments with practice files so you can can follow along & absorb what's being taught.

    David Powers — Online Courses, Classes, Training, Tutorials on Lynda

    Nancy

    Nancy O'Shea— Product User & Community Expert