0

I'm trying to get two dates based on a selection from a previous dropdown.

Functionally, the process is working. However, I am getting a result of {object Object} instead of the dates.

PHP

$data=array();

while($row=mysql_fetch_array($result)) {

    $data[]=array('start_date'=>date("m/d/Y",strtotime($row['start_date'])));
    //log_msg(print_r($data, true));
}

echo (json_encode($data));

jQuery

 .ajax({
        type: 'POST',
        url: 'admin_workshop_update_retrieve.php',
        dataType: 'json',
        data: 'value=' + choice,
        success: function(data) {
            $.each(data, function(k, v) {
            $("#workshop_dates").append($('<option value ="' + v + '" >' + v + '</option><br>'));
            });
        }
    });

It seems like I'm missing something easy to turn the JSON object into a string or text. Would greatly appreciate any help.

2
  • print the value of console.log(data) Commented Aug 24, 2012 at 5:28
  • tip : use JSON_FORCE_OBJECT parameter with json_encode Commented Aug 24, 2012 at 5:30

1 Answer 1

4

You should treat them as objects:

$("#workshop_dates").append($('<option value ="' + v.start_date + '" >' + v.start_date + '</option><br>'));
Sign up to request clarification or add additional context in comments.

3 Comments

may i know what is the usage of k in function(k,v) ?
@diEcho - data seems to be an array of objects, so in this case k would be the numeric index of the current array element where v is the actual array element at that index. See the $.each() doco.
k is the array index, eg: 0, 1

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.