How to return value using ajax

I have an Ajax file where the code was written to take the values ​​of a user form and then those values ​​are taken in an Ajax function like this:

$ (document) .ready (function () {  
            $ ("# newsletterform"). validate ();  
            $ ('# Submit'). Click (function () {  
                var name = $ ('# newsletter_name'). val ();  
                var email = $ ('# newsletter_email'). val ();  
                 sendValue (email, name);  
            });  
            }); 

Function to pass values ​​and get values ​​from another file:

function sendValue (str, name) {  
                $ .post (  
                "newsletter / subscribe.php", // Ajax file  
                {sendValue: str,  
                  sendVal: name  
                },  
                function (data2) {  
                    $ ('# display'). html (data2.returnValue);  
                },  

    // How you want the data formated when it is returned from the server.
                "json"  
                );  
            }  

and these values ​​are passed to another file called "subscribe.php" which writes the embed code to the database and again I return the value of my first ajax function like this:

echo json_encode (array ("returnValue" => $ msg));  
The msg is ging to contain my message to be displayed.  

But now this works well on localhost, I get the return values ​​above the post correctly, but when I upload it to the server, it produces an error:

data2 is null
[Break on this error] $ ('# display'). Html (data2.returnValue);  

This only gives an error for the return value, but insert, mail sending works fine.
Please provide me with a good solution where I can return the return values ​​without error.
Thanks in advance.

+2


a source to share


4 answers


Use this:



var response = $.ajax({
   type : "POST",
   url : "newsletter/subscribe.php",
   dataType : "json",
   async : false,
   data : "sendValue="+str+"&sendVal="+name
}).responseText;

      

-2


a source


If it works on your development site, I suspect that the error was in your PHP script. Your host might be running an old php version that doesn't have json_encode (). Just call the script manually to check its output. If it requires POST, you can write a form or check the result on your ajax call with FireBug



+1


a source


Without further explanation as to why this is happening, try this:

$(document).ready(function(){  
        $("#newsletterform").validate();  
        $('#Submit').click(function(e){  // added the e paramenter
            var name = $('#newsletter_name').val();  
            var email = $('#newsletter_email').val();  
             sendValue(email,name);
            e.stop(); // dont submit the form normaly
        });  
}); 

      

0


a source


If you have firebug, write data2 to the console and see what it is:

function(data2) {  
    console.log(data2);
    $('#display').html(data2.returnValue);  
}

      

Alternatively, you can use the netbug net panel to see the original php file response (if it has an error, you will see it there).

0


a source