I have prepared one HTML form backed with Jquery and PHP. The form is giving the correct out put in PHp but when the form is submitted, it is not showing the success message & not getting the fields empty. The code is given below:
function sendContact() {
event.preventDefault();
var valid;
valid = validateContact();
if (valid) {
jQuery.ajax({
// input submisssion though Ajax
url: "xxxx.php",
data: 'userName=' + $("#userName").val() + '&userEmail=' + $("#userEmail").val() + '&subject=' + $("#subject").val() + '&content=' + $(content).val(),
type: "POST",
success: function (data) {
// Thankyou message on sucessful submission.
$("#mail-status").html(data);
$('#mail-status').show();
// Clear the form.
$('#userName').val('');
$('#userEmail').val('');
$('#content').val('');
},
error: function () {
}
});
}
}
//error checking
function validateContact() {
var valid = true;
$(".InputBox").css('background-color', '');
$(".info").html('');
if (!$("#userName").val()) {
$("#userName-info").html("(required)");
$("#userName").css('background-color', '#FFFFDF');
valid = false;
}
if (!$("#userEmail").val()) {
$("#userEmail-info").html("(required)");
$("#userEmail").css('background-color', '#FFFFDF');
valid = false;
}
if (!$("#userEmail").val().match(/^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/)) {
$("#userEmail-info").html("(invalid)");
$("#userEmail").css('background-color', '#FFFFDF');
valid = false;
}
if (!$("#content").val()) {
$("#content-info").html("(required)");
$("#content").css('background-color', '#FFFFDF');
valid = false;
}
return valid;
}
The AJAX library used is: https://code.jquery.com/jquery-2.1.1.min.js" type="text/javascript The HTML Code is:
<div id="frmContact">
<div id="mail-status" style="display: none;">Thanking you.</div>
<label style="padding-top:20px;">Name</label><span id="userName-info" class="info"></span><br/>
<input type="text" name="userName" id="userName" class="InputBox"><br>
<label>Email</label><span id="userEmail-info" class="info"></span><br/>
<input type="text" name="userEmail" id="userEmail" class="InputBox"><br/>
<label>Content</label><span id="content-info" class="info"></span><br/>
<textarea name="content" id="content" class="InputBox" cols="25" rows="6"></textarea><br/>
<button name="submit" class="btnAction" onClick="sendContact();">Send</button>
</div>
I have checked no of sites & googled for the correct code but could not find?. Could you one spot the correct one please?