2

I have been successful in returning the json responce from the ajax call to my php script that sends messages. When submitting data to the PHP script, if successful it should return a json string like the following...

{"status":"valid","message":"Your message was send successfully."}

I would like to check if the "status" equals valid and then show a div showing that the message has been sent. How would i go about doing this and this is the code i have so far...

$("#send").click(function() {
        var complete = true;
        $('input#fullname, input#email, input#subject, textarea#message').each(function() {
            if ($(this).val()) {
                $(this).css("background","#121212").css("color","#5c5c5c");
            } else {
                $(this).css("background","#d02624").css("color","#121212");
                complete = false;
            }
        });
        if (complete == true){
            var name = $("input#fullname").val();
            var email = $("input#email").val();
            var subject = $("input#subject").val();
            var message = $("textarea#message").val();
            var data = 'name='+name+'&email='+email+'&subject='+subject+'&message='+message;
            $.ajax({
                type:"POST",  
                url:"resources/includes/contact.php",  
                data:data,
                success:function(data){
                    alert(data);
                }
            });
        }
    });

Thanks,
Nathaniel Blackburn

1
  • i think you are setting the parameters in wrong way, you are using method post and sending the data as query string which is not workable either you need to change the way of sending the data or change the method to get. here is the jquery post documentation. Commented Nov 25, 2011 at 6:36

3 Answers 3

1

you can do it like this

$.ajax({
                type:"POST",  
                url:"resources/includes/contact.php",  
                data:data,
                dataType: 'json',
                success:function(response){
                   if(response.status="valid"){
                        ......
                  }

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

5 Comments

replace dateType: 'json' with dataType: 'json',
success:function(responce){} is one of them as it should be success:function(data){}
@nblackburn I used reponse because he was already using data variable .. to avoid confusion .
@Arfeen the biggest problem is that response isn't an object but a text. You have to parse it first
@Martin I'm already mentioning "dataType: 'json'", this is giving us a response as a json object
0

In your success callback function, add

data = $.parseJSON(data);
if (data.status == "valid"){
     //do stuff
}

Comments

0

change your success function to look like this:

success:function(data)
{
    jsonObject = JSON.parse(data);
    alert(jsonObject.status);
}

2 Comments

Thanks, i will try this out and see what happens :)
Awesome this helps so much, i knew it would be something simple.