0

heres my code so you see what i'm trying to do, which obviously isn't working:

        $('#selector').change(function() {  
        if ($(this).val().length > 0)
        {           
            $.ajax({
                type: "POST",
                url: "ajax.php",
                data: "id="+$(this).val(),
                success: function(data){
                    if (data != 'error')
                    {
                        $('#main_url').val(data[2]);
                        $('#main_url_title').val(data[3]);
                    }
                }
            });
        }
    });

i set up an alert(data); and all it pops up with is "Array". so now my 2 fields are being populated with "r" and "a"...what is the correct way of doing this?

2
  • 2
    The string literal "Array" means that you stringified an array on the PHP side of things. Can we see your PHP code, please? Commented Mar 23, 2011 at 23:54
  • $sql = mysql_query("select * from table where id='".mysql_real_escape_string($_POST['id'])."'"); if (mysql_num_rows($sql) == 0) { echo 'error'; } else { while ($row = mysql_fetch_assoc($sql)) { foreach ($row as $k => $v) $data[] = $v; } echo $data; } Commented Mar 24, 2011 at 0:29

1 Answer 1

0

I think you may encode php array in json data, and then use json with jquery

Add in ajax.php something like echo json_encode($your_php_array)

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

2 Comments

well i know i could always just implode the array via php and then use the split function...just wondering if there was a better way of doing this....
this seems good but now i need a hand with using the json encoded array within the javascript.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.