Skip to main content
Known Participant
November 17, 2018
Answered

Contact form issue and question.

  • November 17, 2018
  • 4 replies
  • 1032 views

Good evening. I'm in the midst of putting up a contact form page with Php. But I can't get my issues resolved to make it go through via test. My link and page is below. I know that there are a few errors outside of the actual form. I'll correct those after. Thanks in advance.

http://michaelguydonphotography.com/contact.php

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

    I see you like to live dangerously and post your e-mail address in plain view.  That may come back to bite you. Robot harvesters love to collect e-mail addresses they find online and sell them to spammers.

    Anyway, this contact form was built with PHP and Bootstrap 3.7.  Your server must support PHP Mail function and you must save this file as a .php file or it won't work.

    On lines 13 and 16, input the correct e-mail addresses for your server's send mail function and your receiving e-mail address.  Don't make any other changes to this form.  Just upload to your server to test it. 

    <?php

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

    // And your server must support PHP Mail() function.

    // Define variables and set to empty values

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

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

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

            $name = $_POST['name'];

            $email = $_POST['email'];

            $phone = $_POST['phone'];

            $message = $_POST['message'];

            $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\nReply-to:$email";

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

    // 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 message is entered

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

    $errMessage = "Please enter your message.";

    } else {

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

    }

    //Check if simple anti-bot test is entered

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

    $errHuman = "Please enter the sum.";

    } else {

         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 &&  !$errMessage && !$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>XYZ Company Contact Form </title>

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

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

    <!-- Bootstrap 3.7 minified CSS-->

    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">

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

    <h3>XYZ Company Contact Form</h3>

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

    <!--begin HTML Form-->

    <form id="myContact" 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="message" class="col-sm-3 control-label"><span class="required">*</span> Message:</label>

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

    <textarea class="form-control" rows="4" name="message" id="message" placeholder="Tell us your story"><?php echo $message;?></textarea>

    <span class="required small"><?php echo $errMessage;?></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>

    </form>

    </div>

    </div>

    <!--jQuery 3.2 minified-->

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

    <!--Bootstrap 3.7 minified-->

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

    </body>

    </html>

    4 replies

    Nancy OShea
    Community Expert
    Nancy OSheaCommunity ExpertCorrect answer
    Community Expert
    November 17, 2018

    I see you like to live dangerously and post your e-mail address in plain view.  That may come back to bite you. Robot harvesters love to collect e-mail addresses they find online and sell them to spammers.

    Anyway, this contact form was built with PHP and Bootstrap 3.7.  Your server must support PHP Mail function and you must save this file as a .php file or it won't work.

    On lines 13 and 16, input the correct e-mail addresses for your server's send mail function and your receiving e-mail address.  Don't make any other changes to this form.  Just upload to your server to test it. 

    <?php

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

    // And your server must support PHP Mail() function.

    // Define variables and set to empty values

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

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

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

            $name = $_POST['name'];

            $email = $_POST['email'];

            $phone = $_POST['phone'];

            $message = $_POST['message'];

            $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\nReply-to:$email";

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

    // 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 message is entered

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

    $errMessage = "Please enter your message.";

    } else {

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

    }

    //Check if simple anti-bot test is entered

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

    $errHuman = "Please enter the sum.";

    } else {

         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 &&  !$errMessage && !$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>XYZ Company Contact Form </title>

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

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

    <!-- Bootstrap 3.7 minified CSS-->

    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">

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

    <h3>XYZ Company Contact Form</h3>

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

    <!--begin HTML Form-->

    <form id="myContact" 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="message" class="col-sm-3 control-label"><span class="required">*</span> Message:</label>

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

    <textarea class="form-control" rows="4" name="message" id="message" placeholder="Tell us your story"><?php echo $message;?></textarea>

    <span class="required small"><?php echo $errMessage;?></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>

    </form>

    </div>

    </div>

    <!--jQuery 3.2 minified-->

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

    <!--Bootstrap 3.7 minified-->

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

    </body>

    </html>

    Nancy O'Shea— Product User & Community Expert
    BenPleysier
    Community Expert
    Community Expert
    November 17, 2018

    Adding to what Energize  and Ben M have said, you do not have a submit button. The button that you do have does nothing, which means that the form is not submitted.

    Change this line

    <button class="btn btn-primary btn-block" name="Send" value="btn btn-primary btn-block">Send</button>

    to

    <button type="submit" class="btn btn-primary btn-block" name="Send" value="btn btn-primary btn-block">Send</button>

    As far as the errors are concerned and taking a random part of the code, there are some glaring mistakes.

    <style type="text/css">

      #image {

    }

      body {

        background-image: url(Jennifer.jpg);

        background-repeat: no-repeat;

        margin-left: 0px;

        margin-top: 0px;

        margin-right: 0px;

        margin-bottom: 0px;

        background-size: cover;

        background-position: bottom bottom;

        height: inherit;

        width: inherit;   

    }

    </div>

      body,td,th {

        font-size: 9rem;

    }

      body,td,th {

        font-size: 0.8rem;

        color: #FFFFFF;

    }

      </style>

    </head>

    <body style="padding-top: 70px">

      <div><style>

        body{

      

        background-size:cover;

        }

        hr{

        background: white;

        }

    .contact-form{

    background:rgba(0,0,0, .6);

    color:white;

    margin-top: 100px;

    padding: 20px;

    box-shadow: 0px 0px 10px 3px grey;

    }

       </style>

      </head>

    <body>

    </div>

    </div>

      <nav class="navbar fixed-top navbar-expand-lg navbar-light bg-light"><a href="http://michaelguydonphotography.com/"><img src="logo camera.fw.png" alt="" width="240" height="53" class="img-fluid"></a> <a class="navbar-brand" href="#"></a>

    Deleting the highlighted parts will go a long way o reviving the document.

    Wappler is the DMXzone-made Dreamweaver replacement and includes the best of their powerful extensions, as well as much more!
    Legacys7Author
    Known Participant
    November 17, 2018

    Ben. Thank you. You are a life savor. The corrections in the padding etc has fixed the position that I couldn't figure out. Now the contact page body form, moved up, which is what I'd wanted, because on smaller computer screens, I had to scroll up vs my much larger screen that I'm working with. I no longer have to do this on the other computers.

    Regarding the submit button? I had figured that, but wasn't 100% sure. The contact form was a code that I'd got from a coder on Youtube. He'd told me that this was just a front end design. That was before I came here looking for answers. With that being said. I'd added the submit code as you'd pointed out. However, I think the issue here is, I don't have a database added yet. I haven't done them in a few years, but my host does have the phpmysql, which I've used years ago. Two questions. The first one is; is there a tutorial on doing a database for contact email forms? My second related question; do I need to have an actual database in order to make this work? Thanks in advance.

    Community Expert
    November 17, 2018

    What is the error, are you not receiving emails? Is the contact form not posting ot your database?  Because this is a PHP page, if there are errors, there isn't much we can see client side. You would need to edit the php.ini file to display errors so that you can see what is going on. If we know what the error is we can help point you in the right direction, but not much we can see with server-side code.

    Legend
    November 17, 2018

    Try and clean up some errors: Showing results for http://michaelguydonphotography.com/contact.php - Nu Html Checker

    The <form> tag hasn't been closed so address that as a priority.

    Once you've cleaned up the code if it still does't work it maybe a problem with the PHP script handling the web form.

    Paul-M - Community Expert