Following are the files i have to get my contactus form working. However, i am not sure what went wrong with my codes such that it keep giving invalid form and message was not send, thus, try again. Just to inform that I have yet to set my mail functionality set up. I only getting my form works and if it is valid, it will bring user to contactthanks.php. Is it due invalid email address? Hope someone can help me notify this error.
contactus.php
<form name="contactForm" action="contactthanks.php" method="post"
id="contactForm" onsubmit="return chkContact(this)">
<div class='input1'>
<div class="help">
<input id="uname" name="name" type="text" placeholder="Name "
value="<?php echo $user_profile["uname"]; ?>">
</div>
<div id="email-label" class="help">
<input id="uemail" name="email" type="text"
placeholder="Email Address " class="input-xlarge"
value="<?php echo $user_profile["uemail"]; ?>">
</div>
<div id="subj-label" class="help">
<input id="utitle" name="title" type="text"
placeholder="Subject " class="input-xlarge"
value="<?php echo $user_profile["utitle"]; ?>">
</div>
<div id="message" class="help">
<textarea id="message" name="message" rows="13" cols="33"
placeholder="Message " class="input-xlarge"></textarea>
</div>
<button id="btnSubmit" type="submit" value="Send"
onclick="return chkContact()">Send</button>
<div id="msgSubmit" class="h3 text-center hidden"></div>
</div>
</form>
<script>
function chkContact(form) {
if(form.uname.value == "" && form.uemail.value == "" && form.utitle.value == "" && form.message.value == "" ) {
alert("Please fill in the required fields: \n\u26AC Name \n\u26AC Email \n\u26AC Subject \n\u26AC Message");
form.uname.focus();
return false;
}
if(form.uname.value == "") {
alert("Error: Name cannot be blank!");
form.uname.focus();
return false;
}
if(form.uemail.value == "") {
alert("Error: Email Address cannot be blank!");
form.uemail.focus();
return false;
}
re = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
if(!re.test(form.uemail.value)) {
alert("Error: Invalid email address!");
form.uemail.focus();
return false;
}
if(form.utitle.value == "") {
alert("Error: Subject cannot be blank!");
form.utitle.focus();
return false;
}
if(form.message.value == "") {
alert("Error: Message cannot be blank!");
form.message.focus();
return false;
}
var uname = document.getElementById('uname').value;
var uemail = document.getElementById('uemail').value;
var utitle = document.getElementById('utitle').value;
var message = document.getElementById('message').value;
var dataString='uname='+ uname+'&uemail='+uemail+'&utitle='+utitle+'&message='+message;
$.ajax({
type:"post",
url: "<?php echo $base_url; ?>ajax-helper/post-email.php",
data:dataString,
cache:false,
success: function(data){
if (/^\s*SUCCESS\s*$/.test(data)) {
window.location = '<?php echo $base_url; ?>index?success=true';
}
else{
alert(data);
}
}
});
return false;
}
</script>
post-email.php
<?php
$uname = Trim ( stripslashes ( $_POST ['name'] ) );
$uemail = Trim ( stripslashes ( $_POST ['email'] ) );
$utitle = Trim ( stripslashes ( $_POST ['title'] ) );
$message = Trim ( stripslashes ( $_POST ['message'] ) );
$EmailTo = "nurainiyakob95@live.com";
$Subject = "New Message Received";
// prepare email body text
$Body = "<br>";
$Body .= "Name: ";
$Body .= $uname;
$Body .= "\n<br>";
$Body .= "Email: ";
$Body .= $uemail;
$Body .= "\n<br>";
$Body .= "Subject: ";
$Body .= $utitle;
$Body .= "\n<br>";
$Body .= "Message: ";
$Body .= $message;
$Body .= "\n<br>";
// send email
// $url = 'http://52.76.105.171/apis/mailerServiceJson/tos_emailer.php';
// $fields = array (
// 'BASE_URL' => $base_url,
// 'BRAND_NAME' => $PROJECT_NAME,
// 'RECEIVER_EMAIL' => $EmailTo,
// 'SENDER_EMAIL' => $email,
// 'RECEIVER_NAME' => '...',
// 'SENDER_NAME' => $name,
// 'SUBJECT' => $Subject,
// 'CONTENT' => $Body
// );
$success = mail ( $EmailTo, $Subject, $Body, "From: <$uemail>" );
// $fields_string = json_encode ( $fields );
// $ch = curl_init ();
// curl_setopt ( $ch, CURLOPT_CUSTOMREQUEST, "POST" );
// curl_setopt ( $ch, CURLOPT_SSL_VERIFYHOST, false );
// curl_setopt ( $ch, CURLOPT_SSL_VERIFYPEER, false );
// curl_setopt ( $ch, CURLOPT_URL, $url );
// curl_setopt ( $ch, CURLOPT_POST, count ( $fields ) );
// curl_setopt ( $ch, CURLOPT_POSTFIELDS, $fields_string );
// curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
// curl_setopt ( $ch, CURLOPT_HTTPHEADER, array (
// 'Content-Type: application/json',
// 'Content-Length: ' . strlen ( $fields_string )
// ) );
// $result = curl_exec ( $ch );
// curl_close ( $ch );
if ($success) {
echo "Message sent!";
} else {
echo "Message not sent! Try again.";
// header('location: contactus.php');
}
?>
contactthanks.php
<div class="six columns">
<f7>Your message has been sent!</f7>
<!-- <br> -->
<br> <a href="contactus.php" style="float: left;">◄ Back to
Contact Form |</a> <a href="home.php" style="float: right;"> Back
to Homepage ►</a>
</div>