I am having a problem when I submit a contact form via Ajax to a PHP script. I have used the jQuery/Ajax function previously as well as the contact script, however on this occaison the process runs as far as the ajax request and then returns the error: empty string.
$(document).ready(function(){
jQuery("#sendmail").click(function(){
var name = $("#name").val();
var phone = $("#phone").val();
var email = $("#email").val();
var text = $("#message").val();
//var datastr ='name=' + name + '&mail=' + mail + '&subject=' + subject + '&text=' + text;
var datastr ='name=' + name + '&phone=' + phone + '&email=' + email + '&text=' + text;
$("#form-div").css("float", "left");
$("#form-div").html("<p>Thank you for contacting Stillframe Photography.</p><p>Please wait for just a few moments while your enquiry is being sent.</p>");
$("#form-div").fadeIn("slow");
alert(datastr);
jQuery.ajax({
type: "POST",
url: "contact.script.php",
data: datastr,
success: function(html){
$("#response").fadeIn("slow");
$("#response").html(html);
//setTimeout('$("#response").fadeOut("slow")',2000);
}
,error: function (XMLHttpRequest, textStatus, errorThrown) {
$("#response").html('there was an error');
console.log('error thrown');
console.log(errorThrown);
}
});
});
});
You can see the page in operation at http://www.stillframe.com.au/test.php and you can see the form without the page media at at the same link above but test2.php
I've also added the script without any other jQuery to the same url but test3.php which posts directly to the contact PHP script to confirm there are no errors in that script.
SOLVED: Removed the base href tag from the head and the script now works fine.
Instead of using field by field, use the FORM ID <form id="myform">
and serialize it. Try this:
$(document).ready(function(){
jQuery("#sendmail").click(function(){
jQuery.ajax({
type: "POST",
url: "contact.script.php",
data: $('#myform').serialize(),
cache: false,
success: function(output) {
$("#form-div").css("float", "left");
$("#form-div").html("<p>Thank you for contacting Stillframe Photography.</p><p>Please wait for just a few moments while your enquiry is being sent.</p>");
$("#form-div").fadeIn("slow");
$("#response").html(output);
$("#response").fadeIn("slow");
//setTimeout('$("#response").fadeOut("slow")',2000);
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
$("#response").html('there was an error');
console.log('error thrown');
console.log(errorThrown);
}
});
});
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With