Hello there Web Developers, I am working on building a floating contact form using html, css, php(ajax) and downloaded a couple of jQuery files for styling and floating which works perfectly. For some reason, when I input my information into the form, I am getting a "parseerror" messagejust above the contact form after hitting submit in my index.html file. i first though that just like css files are meant to be called from html if external, the same should count with php. However I read somehwere and noticed myself also that the javascript inside the index calls the php file anyway (where I have highlited in red) and therefore a special call is not needed.
hmmm a parseerror ...help would be really appreciated as I am trying my best to make this work. The following are the two html and php classes have used:
HTML:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script type="text/javascript" src="js/jquery-1.10.2.min.js"></script>
<script type="text/javascript" src="js/jquery.validate.min.js"></script>
<script type="text/javascript" src="js/stickyfloat.js"></script>
<script type="text/javascript">
$( document ).ready(function() {
$('#contact-wrapper').stickyfloat({ duration: 400, cssTransition: true, offsetY:100}); //stickyfloat contact form
$("#contact-btn").click(function() { //smoothly slide open/close form
var floatbox = $("#floating-contact-wrap");
if (floatbox.hasClass('visiable')){
floatbox.animate({"right":"-340px"}, "fast").removeClass('visiable');
}else{
floatbox.animate({"right":"0px"}, "fast").addClass('visiable');
}
});
$("#message-send-btn").click(function() { //try sending email
//validate the form
var validator = $( "#floating-contact").validate();
var isValid = validator.form();
if(isValid){ //is everyting valid? proceed
//collect values from input fields
var user_name = $('.floating-contact-inner input[name=name]').val();
var user_email = $('.floating-contact-inner input[name=email]').val();
var user_message = $('.floating-contact-inner textarea[name=message]').val();
//prepare ajax data
post_data = {'s_name':user_name, 's_email':user_email, 's_message':user_message};
$(this).hide(); //hide submit button
$('#ajax-loading-image').show(); //show loading image
$.post('ajax_response.php', post_data, function(data){
validator.resetForm();
//load success massage in #result div element,
$("#result").hide().html('<div class="success">'+data+'</div>').slideDown();
$('#floating-contact').hide(); //show submit button
}).fail(function(err) { //load any error data
$("#result").hide().html('<div class="error">'+err.statusText+'</div>').slideDown();
$("#message-send-btn").show(); //show submit button
$('#ajax-loading-image').hide(); //hide loading image
});
}else{
$("#message-send-btn").show(); //show submit button
$('#ajax-loading-image').hide(); //hide loading image
}
});
});
</script>
<link href="style.css" rel="stylesheet" type="text/css" />
</head>
<body>
<!-- floating contact form markup -->
<div id="contact-wrapper">
<div class="floating-contact-inner" id="floating-contact-wrap">
<div id="contact-btn"> </div>
<div id="result"></div>
<form action="" method="post" id="floating-contact" >
<label>
<span>Name</span>
<input id="name" type="text" name="name" placeholder="Your Full Name" required />
</label>
<label>
<span>Email</span>
<input id="email" type="email" name="email" placeholder="Valid Email Address" email required />
</label>
<label>
<span>Message</span>
<textarea id="message" name="message" placeholder="Your Message to Us" required></textarea>
</label>
<label>
<span> </span>
<input type="button" class="button" id="message-send-btn" value="Send" />
<img id="ajax-loading-image" style="display:none" src="image/ajax-loader.gif" border="0" />
</label>
</form>
</div>
</div>
<!-- end floating contact form markup -->
</body>
</html>
PHP:
<?php
if($_POST)
{
//check if its an ajax request, exit if not
if(!isset($_SERVER['HTTP_X_REQUESTED_WITH']) AND strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) != 'xmlhttprequest') {
die();
}
$to_Email = "rashid_92@hotmail.co.uk"; //Replace with recipient email address
$subject = 'Ah!! My email from Somebody out there...'; //Subject line for emails
//check $_POST vars are set, exit if any missing
if(!isset($_POST["s_name"]) || !isset($_POST["s_email"]) || !isset($_POST["s_message"]))
{
die();
}
//Sanitize input data using PHP filter_var().
$user_Name = filter_var($_POST["s_name"], FILTER_SANITIZE_STRING);
$user_Email = filter_var($_POST["s_email"], FILTER_SANITIZE_EMAIL);
$user_Message = filter_var($_POST["s_message"], FILTER_SANITIZE_STRING);
//additional php validation
if(strlen($user_Name)<4) // If length is less than 4 it will throw an HTTP error.
{
header('HTTP/1.1 500 Name is too short or empty!');
exit();
}
if(!filter_var($user_Email, FILTER_VALIDATE_EMAIL)) //email validation
{
header('HTTP/1.1 500 Please enter a valid email!');
exit();
}
if(strlen($user_Message)<5) //check emtpy message
{
header('HTTP/1.1 500 Too short message! Please enter something.');
exit();
}
//proceed with PHP email.
$headers = 'From: '.$user_Email.'' . "rn" .
'Reply-To: '.$user_Email.'' . "rn" .
'X-Mailer: PHP/' . phpversion();
@$sentMail = mail($to_Email, $subject, $user_Message .' -'.$user_Name, $headers);
if(!$sentMail)
{
header('HTTP/1.1 500 Couldnot send mail! Sorry..');
exit();
}else{
echo '<h2>Email Sent!</h2>';
echo 'Hi '.$user_Name .', Thank you for your email! ';
echo 'Your email has already arrived in my Inbox, all I need to do is Check it.';
}
}
?>