Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery AJAX form error. Empty string

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.

like image 381
Mark Lawrence Avatar asked Aug 25 '11 23:08

Mark Lawrence


1 Answers

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); 
            }
        });
    });
});
like image 181
Book Of Zeus Avatar answered Oct 16 '22 11:10

Book Of Zeus