0

in this my code data is undefined why? i can't access to data.message.

$.post("actions.php", {name_madan: madan , postaction:'sabte_madan' },
    function(data){
    if (data.success)
           alert(data.message);
    });

actions.php

case 'sabte_madan':
    $query="SELECT * from maadan WHERE name_madan='{$_POST['name_madan']}'";
    $result=$db->loadAssoc($db->setQuery($query));
    if ( !$result )
    {           
    $data['success']=true;  $data['message']='ok';
    }
    else $data['success']=false;    $data['message']='no';              

break;

if ( isset ($data)) echo json_encode($data);
5
  • 2
    Looks like $data is only being set if !$result. Are you missing the else part of the if statement? Commented Oct 26, 2012 at 15:21
  • 1
    You need to add ,'json' to your $.post to tell jQuery to parse it as JSON. Commented Oct 26, 2012 at 15:22
  • May be this can help if ( !$result ) { $data['success']=false; $data['message']='ok'; } else { $data['success']=true; } Commented Oct 26, 2012 at 15:23
  • Anyway, can you insert some debugging into this case block? Commented Oct 26, 2012 at 15:24
  • Also, I wonder how exactly do you check for data undefinedness. Or it's data.success that is not true here? Also, do you understand that your code is vulnerable to SQL injections? Commented Oct 26, 2012 at 15:28

1 Answer 1

1

you miss 'json' data type

$.post("actions.php", {name_madan: madan , postaction:'sabte_madan' },
    function(data){
        if (data.success)
           alert(data.message);
    },'json');
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.