tags:

views:

133

answers:

4

Hello.
I have Ajax file in which code has written to accept values form user and then these values are taken in a Ajax function as follows:

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

The function for passing values and getting values from other 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" in which insertion code to database is written and again I return the value to my first ajax function as follows:

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

But now, this works fine on localhost, I get the return values nad message properly but when I upload it on server this gives me an error as:

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

This only gives error for return value but insertion, sending mail functionality works fine.
Please provide me with a good solution wherein I can be able to get back the return values without any error.
Thanks in advance.

A: 

Use that:

var response = $.ajax({
   type : "POST",
   url : "newsletter/subscribe.php",
   dataType : "json",
   async : false,
   data : "sendValue="+str+"&sendVal="+name
}).responseText;
sundowatch
ThiefMaster
He wants to get responseText. Yes there are many way to do it.
sundowatch
A: 

If it works on your development site, I suspect the error to be in your PHP script. Your host might run an ancient php version which does not have json_encode(). Simply call the script manually to check its output. If it requires POST you could write a form or check the result to your ajax call with FireBug

ThiefMaster
A: 

Without additional explanation 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
        });  
}); 
Marek
A: 

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

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

In addition, you can use firebug net panel to see your php file raw response (if it has error - you will see it there).

Zyava