Search code examples
jqueryhrefradix

jQuery POST issue with base href


I have to use <base href="http://www.mysite.com/it/"/> on my site because I am using apache mod_rewrite, that causes issues if I do not put the base href tag.

But the problem is now coming with this jQuery function:

$(document).ready(function(){
    $("#submit_newsletter").click(function(){                                      
        var hasError = false;
        var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;

            var nameVal = $("#name").val();
        if(nameVal == '') {
                        $("#name").addClass("highlightError").delay(4000).queue(function(){
        $(this).removeClass("highlightError");
        $(this).dequeue();
    });
        hasError = true;
                 }

               var subnameVal = $("#subname").val();
        if(subnameVal == '') {
                        $("#subname").addClass("highlightError").delay(4000).queue(function(){
        $(this).removeClass("highlightError");
        $(this).dequeue();
    });
            hasError = true;
        }


        var emailVal = $("#email").val();
        if(emailVal == '') {
            $("#email").addClass("highlightError").delay(4000).queue(function(){
        $(this).removeClass("highlightError");
        $(this).dequeue();
    });

   hasError = true;
        } else if(!emailReg.test(emailVal)) {
                        $("#email").addClass("highlightError").delay(4000).queue(function(){
        $(this).removeClass("highlightError");
        $(this).dequeue();
    });
        hasError = true;
        }


        var messageVal = $("#message").val();
        if(messageVal == '') {
                        $("#message").addClass("highlightError").delay(4000).queue(function(){
        $(this).removeClass("highlightError");
        $(this).dequeue();
    });

            hasError = true;
        }



        if(hasError == false) {
            $.post("mail.php",
                { name: nameVal, subname: subnameVal, email: emailVal, message: messageVal },
                    function(data){
                        $("#results").text("Messaggio Inviato.").delay(4000).queue(function(){
       $("#results").text("")
        $(this).dequeue();
    });

                                                $('#newsletter')[0].reset();

                    }
                 );
        }

        return false;
    });     

});

When I remove the base href tag it works just fine, when I use it ( and I repeat, I must use it ) the above jQuery function stops to work. It does not give any error, simply it does not pass any value to the mail.php file, and does not return any result.

How can I fix this jQuery issue with the base href tag?

Thanks.


Solution

  • I think you are not rewriting your querystring in the rewrite. The url will be something like this now, since jQuery $.post default type is the GET method:

    mysiste.com/it/?param1=value1 
    

    You can use the type : "post" in jQuery to not use the querystring.

    The RewriteRule if you want to keep your GET method should be something like this:

    RewriteRule ^/it/$ /mail.php [QSA,L]