2

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.

1
  • 1
    As this is your 9th question, please take responsibility for formatting your own code correctly. Click the little orange [?] on the Ask a Question page, which takes you here: stackoverflow.com/editing-help, for details. Commented May 10, 2010 at 5:23

4 Answers 4

1

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

Sign up to request clarification or add additional context in comments.

Comments

0

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
        });  
}); 

Comments

0

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).

Comments

-2

Use that:

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

3 Comments

Why should he do that? Passing the arguments as a string requires him to encode them manually (or things will break if 'str' contains the '&' character). Additionally, using synchronous calls is not really good since it will block the page (iirc it even used to block the whole browser).
He wants to get responseText. Yes there are many way to do it.
Never use async:false if you don't realy really really really really really need it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.