1

Hello I have an ajax request that submits a form and sends and email, if the email is submitted successfully, I encode a PHP array that looks like this,

$success = array("state" => "Email Sent");

I am then checking the state of data in my ajax request to see if state matches "Email Sent" however when I alert(data) i get undefined, what am I doing wrong? Below is my javascript,

$.ajax({
                    url: "<?php echo base_url(); ?>home/callback",
                    data: $("#callback").serialize(),
                    type: "POST",
                    dataType: "JSON",
                    success: function(data){
                        $("#fancybox-content div").html(data);
                        alert(data.state);
                    }
                });
sea_1987
  • 2,812
  • 12
  • 43
  • 68

4 Answers4

0

Try to get the html value of the 'fancybox-content' div. $('#fancybox-content').html();

Ammu
  • 4,837
  • 9
  • 32
  • 34
0

If you alert is failing then are you sure your json is valid? Can you paste it into your question. You can validate it here online at JsonLint.

redsquare
  • 77,122
  • 20
  • 149
  • 156
0

how do you return the $succes array?
You should echo the array in a way that would preserve it's format and would be understandable to javascript (not print_r or var_dump):

echo json_decode($success);

that will return a json string representing your php array, which js automatically converts into an object.
Hope this helps.

gion_13
  • 40,487
  • 10
  • 96
  • 107
0

use

json_decode($success, true); 

When TRUE, returned objects will be converted into associative arrays.

diEcho
  • 52,196
  • 40
  • 166
  • 239