Jump to content

Trying To Get PHP To Email A Form


TheNyru

Recommended Posts

Hello all,

 

Fist post and just to let you know I am VERY new to PHP. I have a form which I would like to send me an email with all the form data when the user click the submit button. I am sure the code it terribly wrong so would appreciate all the help I can get haha.

 

HTML

 

<!DOCTYPE html>

<html dir="ltr" lang="en-US"><head>
    <meta charset="utf-8">
    <title>Bonners Music Academy</title>
    <meta name="viewport" content="initial-scale = 1.0, maximum-scale = 1.0, user-scalable = no, width = device-width">
 
    <link rel="stylesheet" href="style.css" media="screen">
    <!--[if lte IE 7]><link rel="stylesheet" href="style.ie7.css" media="screen" /><![endif]-->
    <link rel="stylesheet" href="style.responsive.css" media="all">
 
 
    <script src="jquery.js"></script>
    <script src="script.js"></script>
    <script src="script.responsive.js"></script>
<meta name="description" content="Why not enquire about lessons at Bonners Music Academy.">
<meta name="keywords" content="Bonners, Music, School, Guitar, Bass, Drums, Drum, Piano, Keyboard, Lessons, Acoustic">
 
 
<style>.art-content .art-postcontent-0 .layout-item-0 { padding-right: 10px;padding-left: 10px;  }
.ie7 .post .layout-cell {border:none !important; padding:0 !important; }
.ie6 .post .layout-cell {border:none !important; padding:0 !important; }
 
</style></head>
<body>
<div id="art-main">
    <div class="art-sheet clearfix">
<header class="art-header">
 
 
    <div class="art-shapes">
 
            </div>
 
 
 
 
                
                    
</header>
<nav class="art-nav">
    <ul class="art-hmenu"><li><a href="home.html" class="">Home</a></li><li><a href="about-us.html" class="">About Us</a></li><li><a href="teachers.html" class="">Teachers</a></li><li><a href="prices.html" class="">Prices</a></li><li><a href="term-dates.html" class="">Term Dates</a></li><li><a href="enquiries.html" class="active">Enquiries</a></li><li><a href="contact-us.html">Contact Us</a></li><li><a href="instrument-sales.html">Instrument Sales</a></li></ul> 
    </nav>
<div class="art-layout-wrapper">
                <div class="art-content-layout">
                    <div class="art-content-layout-row">
                        <div class="art-layout-cell art-content"><article class="art-post art-article">
                                
                                                
                <div class="art-postcontent art-postcontent-0 clearfix"><div class="art-content-layout">
    <div class="art-content-layout-row">
    <div class="art-layout-cell layout-item-0" style="width: 100%" >
        <h1 style="margin-bottom: 20px; background-color: transparent; color: rgb(73, 64, 62); text-shadow: rgba(0, 0, 0, 0.496094) 0px -1px 0px, rgba(255, 255, 255, 0.34375) 0px 1px 0px;"><span style="font-family: Tahoma; font-size: 20px; text-shadow: rgba(23, 23, 23, 0.496094) 1.4px 1.4px 0px; color: #161616;">Enquire About Lessons Online</span></h1>
    </div>
    </div>
</div>
<div class="art-content-layout">
    <div class="art-content-layout-row">
    <div class="art-layout-cell layout-item-0" style="width: 100%" >
        <p><span style="font-family: Tahoma;"><span style="background-color: transparent; font-weight: bold; color: #161616;">Fill in the Form Now and You Will be Contacted with Current Availability.</span></span></p>
        <p><span style="font-family: Tahoma;"><span style="background-color: transparent; color: #161616;">Please note: By completing this form you are under no obligation to sign up for lessons until you have agreed your confirmed class time.</span></span><br></p>
    </div>
    </div>
</div>
<div class="art-content-layout">
    <div class="art-content-layout-row">
    <div class="art-layout-cell layout-item-0" style="width: 100%" >
        <center>
        <form action="./files/mailer.php" method="post">
        <div><label>Students First Name & Surname</label> *<br>
        <input class="form-input-field" type="text" value="" name="element0" size="100"><br>
        <br>
        <label>Students Age:</label> *<br>
        <input class="form-input-field" type="text" value="" name="element1" size="100"><br>
        <br>
        <label>Students Full Address:</label> *<br>
        <textarea class="form-input-field" name="element2" rows="8" cols="103"></textarea>
        <br>
        <br>
        <label>Students Post Code:</label> *<br>
        <input class="form-input-field" type="text" value="" name="element3" size="100"><br>
        <br>
        <label>Parent/Guardians Name & Surname (If pupil under 18):</label><br>
        <input class="form-input-field" type="text" value="" name="element4" size="100"><br>
        <br>
        <label>Home Phone Number:</label> *<br>
        <input class="form-input-field" type="text" value="" name="element5" size="100"><br>
        <br>
        <label>Mobile Number:</label><br>
        <input class="form-input-field" type="text" value="" name="element6" size="100"><br>
        <br>
        <label>Your Email Address:</label> *<br>
        <input class="form-input-field" type="text" value="" name="element7" size="100"><br>
        <br>
        <label>Which Instrument Are You Interested In?</label> *<br>
        <select name="element8">
        <option value="Acoustic Guitar">Acoustic Guitar</option>
        <option value="Electric Guitar">Electric Guitar</option>
        <option value="Bass Guitar">Bass Guitar</option>
        <option value="Drums">Drums</option>
        <option value="Piano">Piano</option>
        <option value="Keyboard">Keyboard</option>
        <option value="Ukulele">Ukulele</option>
        <option value="Music Theory">Music Theory</option>
        </select><br>
        <br>
        <label>What Lesson Type Are You Interested In?</label> *<br>
        <select name="element9">
        <option value="Beginners Lesson Intro Course (Group)">Beginners Lesson Intro Course (Group)</option>
        <option value="Private Lessons">Private Lessons</option>
        </select><br>
        <br>
        <label>Any Other Relevant Info (i.e Previous Experience):</label><br>
        <textarea class="form-input-field" name="element10" rows="8" cols="103"></textarea>
        <br>
        <br>
        <div style="display: none;"><label>Spam Protection: Please don't fill this in:</label> 
        <textarea name="comment" rows="1" cols="1"></textarea></div>
        <input class="form-input-button" type="reset" name="resetButton" value="Reset"> <input class="form-input-button" type="submit" name="submitButton" value="Submit"></div>
        </form>
        </center>
    </div>
    </div>
</div>
</div>
</article></div>
                    </div>
                </div>
            </div>
    </div>
<footer class="art-footer">
  <div class="art-footer-inner">
<p>Copyright © 2013, Bonners Music Academy All Rights Reserved.</p>
  </div>
</footer>
 
</div>
 
 
</body></html>

 

PHP

 

 

<?php
if(isset($_POST['submit'])) {
$to = "[email protected]";
$subject = "Music School Enquiry";

// data the visitor provided
$name_field = filter_var($_POST['element0'], FILTER_SANITIZE_STRING);
$age_field = filter_var($_POST['element1'], FILTER_SANITIZE_STRING);
$address_field = filter_var($_POST['element2'], FILTER_SANITIZE_STRING);
$postcode_field = filter_var($_POST['element3'], FILTER_SANITIZE_STRING);
$parent_field = filter_var($_POST['element4'], FILTER_SANITIZE_STRING);
$homeno_field = filter_var($_POST['element5'], FILTER_SANITIZE_STRING);
$mobileno_field = filter_var($_POST['element6'], FILTER_SANITIZE_STRING);
$email_field = filter_var($_POST['element7'], FILTER_SANITIZE_STRING);
$instrument_field = filter_var($_POST['element8'], FILTER_SANITIZE_STRING);
$type_field = filter_var($_POST['element9'], FILTER_SANITIZE_STRING);
$other_field = filter_var($_POST['element10'], FILTER_SANITIZE_STRING);

//constructing the message
$body = " Students First Name & Surname: $name_field\n\n Students Age: $age_field\n\n Students Full Address: $address_field\n\n Students Post Code: $postcode_field\n\n Parent/Guardians Name & Surname (If pupil under 18): $parent_field\n\n Home Phone Number: $homeno_field\n\n Mobile Number: $mobileno_field\n\n Email Address: $email_field\n\n Which Instrument: $instrument_field\n\n Lesson Type: $type_field\n\n Other Information: $other_field";

// ...and away we go!
mail($to, $subject, $body);

// redirect to confirmation
header('Location: http://www.bonnersmusicschool.co.uk/thank-you.html');
} else {
// handle the error somehow
}
?>

 

Thank you for any help you can give :)

Link to comment
https://forums.phpfreaks.com/topic/283048-trying-to-get-php-to-email-a-form/
Share on other sites

Change,

isset($_POST['submit']))

to

isset($_POST['submitButton'])

// add an exit command to terminate the script
// when the page has been redirected 

// redirect to confirmation
header('Location: http://www.bonnersmu...ank-you.html');

exit;

You should also spend time to learn more about debugging process!

For what it's worth, the <label> tags need to be attached to the form fields. For more information, see:

http://www.cyberscorpion.com/2012-02/making-html-forms-more-accessible-and-improving-usability-with-the-label-tag/

 

Also, there is a specific filter for email addresses:

http://php.net/manual/en/filter.examples.validation.php

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.