Last active
December 19, 2015 13:58
-
-
Save ElmahdiMahmoud/5965872 to your computer and use it in GitHub Desktop.
php: contact form
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<form id="contactForm" method="post"> | |
<fieldset class="grid halves"> | |
<div class="grid-item"> | |
<ol> | |
<li> | |
<label class="label--has-placeholder" for="firstName">First Name</label> | |
<input type="text" placeholder="First Name" name="firstName" id="firstName"> | |
</li> | |
<li> | |
<label class="label--has-placeholder" for="lastName">Last Name</label> | |
<input type="text" placeholder="Last Name" name="lastName" id="lastName"> | |
</li> | |
<li> | |
<label class="label--has-placeholder" for="emailAddress">Email</label> | |
<input class="js-validate-email" type="text" placeholder="Email" name="email" id="emailAddress"> | |
</li> | |
<li> | |
<label class="label--has-placeholder" for="messageText">Text</label> | |
<textarea name="message" placeholder="Text" id="messageText"></textarea> | |
</li> | |
<li> | |
<button class="button button-primary" type="submit">Submit</button> | |
</li> | |
</ol> | |
</div> | |
<!-- .grid-item --> | |
</fieldset> | |
<!-- .halves --> | |
<h3 class="notification"></h3> | |
</form> | |
<!-- .form-with-placeholders --> | |
<script src="http://code.jquery.com/jquery-1.9.1.js"></script> | |
<script> | |
$('#contactForm').on('submit', function () { | |
var | |
fname = $('[name=firstName]').val(), | |
lname = $('[name=lastName]').val(), | |
email = $('[name=email]').val(), | |
message = $('[name=message]').val(); | |
if ($('#contactForm input[type="text"]').val() == "" || $("#contactForm textarea").val() == "") { | |
$('#contactForm input[type="text"],#contactForm textarea').addClass('fielderror'); | |
} else { | |
$.ajax({ | |
type: "POST", | |
url: "functions.php", | |
data: { | |
fname: fname, | |
lname: lname, | |
email: email, | |
message: message | |
} | |
}).success(function (html) { | |
$('h3.notification').html('Your message has been sent successfuly, we\'ll feed you back as soon as we can. Thank you').addClass('successmsg'); | |
}).error(function () { | |
$('h3.notification').html('Oops something went wrong!').addClass('errormsg'); | |
}); | |
$('#contactForm fieldset').animate({ | |
'marginLeft': '800px' | |
}, 300, 'swing', function () { | |
$('#contactForm fieldset').css({ | |
'display': 'none', | |
'margin-left': '0' | |
}).html().fadeIn(); | |
}); | |
} | |
return false; | |
}); | |
$('#contactForm input[type="text"],#contactForm textarea').on('click', function () { | |
$('#contactForm input[type="text"],#contactForm textarea').removeClass('fielderror'); | |
}); | |
</script> | |
<?php | |
if(isset($_POST['fname']) && isset($_POST['lname']) && isset($_POST['email']) && isset($_POST['message'])) { | |
$to = '[email protected]'; | |
$subject = 'Message from ' . $_POST['fname'] . ' (' .$_POST['email']. ')'; | |
$message = $_POST['message']; | |
$headers = "From: " . $_POST['email'] . "\r\n"; | |
$sent = mail($to, $subject, $message, $headers); | |
if($sent) { | |
echo '<h2 class="success">Your message has been sent!</h2>'; | |
} else { | |
echo '<h2 class="error">Oops something goes wrong!</h2>'; | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment