I am having a problem when I submit a contact form via Ajax to a PHP script. I previously used the jQuery / Ajax function as well as the contact script, however in this case the process is executed before the ajax request and then returns an 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 the mode of http://www.stillframe.com.au/test.php , and you can see the form without the page medium using the same link above, but test2.php
I also added a script without any other jQuery to the same url, but test3.php, which is sent directly to the contact of the PHP script to confirm that there are no errors in this script.
SOLVED: removed the base href tag from the head, and the script now works fine.
source share