I am having trouble with ajaxForm plugin. I want it to display a result message to the user without leaving the page.
The problem is that when I submit the form, instead of prepending the alert into the .jumbotron.modded (this is the div container of the form), it navigates away to supportForm.php and just displays the echoed number.
This does work well sometimes, but most of the times it just navigates away to supportForm.php . I cannot understand why it does not always have the same behaviour.
Here's the HTML form:
<form id="supportForm" action="supportForm.php" method="post" style="padding-top:10px">
<div class="form-group">
<input type="text" name="subject" id="subject" placeholder="Subject" class="form-control" required>
</div>
<div class="form-group">
<textarea class="form-control" rows="4" name="comment" id="comment" placeholder="Write your message here" required></textarea>
</div>
<button type="submit" class="btn btn-upload" style="margin-top:20px;float:right;" value="">SEND<img src="img/plus.png" style="margin-bottom:5px;padding-left:5px;" /></button>
</form>
Here's the Javascript code:
window.onload = function()
{
$('#supportForm').ajaxForm({
success: function(res) {
console.log(res);
if(res=='1')
{
$(".jumbotron.modded").prepend('<div class="alert alert-success alert-dismissible" role="alert"> <button type="button" class="close" data-dismiss="alert"><span aria-hidden="true">×</span><span class="sr-only">Close</span></button>Successfully sent. Thank you</div>');
}else{
switch(res)
{
case '0':
$(".jumbotron.modded").prepend('<div class="alert alert-danger alert-dismissible" role="alert"> <button type="button" class="close" data-dismiss="alert"><span aria-hidden="true">×</span><span class="sr-only">Close</span></button>Error 1. Please contact mail@asd.com<</div>');
break;
case '-1':
$(".jumbotron.modded").prepend('<div class="alert alert-danger alert-dismissible" role="alert"> <button type="button" class="close" data-dismiss="alert"><span aria-hidden="true">×</span><span class="sr-only">Close</span></button>Error 2. Please contact mail@asd.com<</div>');
break;
case '-2':
$(".jumbotron.modded").prepend('<div class="alert alert-danger alert-dismissible" role="alert"> <button type="button" class="close" data-dismiss="alert"><span aria-hidden="true">×</span><span class="sr-only">Close</span></button>Error 3. Please contact mail@asd.com</div>');
break;
case '-3':
$(".jumbotron.modded").prepend('<div class="alert alert-danger alert-dismissible" role="alert"> <button type="button" class="close" data-dismiss="alert"><span aria-hidden="true">×</span><span class="sr-only">Close</span></button>Error 4. Please contact mail@asd.com<</div>');
break;
}
}
}
});
}
Here's the PHP code:
if ($row = $db->query("SELECT user FROM tblusers WHERE userId='".$fromId."'")->fetch_array())
{
$fromMail1 = $row['user'];
if($row2 = $db->query("SELECT name,lastname,email FROM tblprofile WHERE userId='".$fromId."'")->fetch_array())
{
$fromMail2 = $row2['email'];
$fromName = $row2['name']." ".$row2['lastname'];
if ($db->query("INSERT INTO tblSupports (supportTitle,supportComment,userId) VALUES ('".$title."','".$comment."','".$fromId."')"))
{
if(sendEmail($fromMail1,$fromMail2,$fromName,$titleClean,$commentClean))
{
echo 1;
}else{// (error 1)
echo 0;
}
}else{//(error 2)
echo -1;
}
}else{//(error 3)
echo -2;
}
}else{//(error 4)
echo -3;
}
How can I solve this? Thank you very much