1

Hello I was wondering if this is possible?

$results = $stmt->fetchAll();
    $recent_posts = array();
    foreach ($results as $row){
        $post = array(
            'username' => $row['username'],
            'steam' => $row['steam'],
            'skill' => $row['skill'],
            'description' => $row['description'],
            'date' => $row['date'],
        );
        array_push($recent_posts, $post);
    }
    return json_encode($recent_posts);

If this is possible how exactly do I access it in jquery? $

('#order_by').on('change', function(){ 
        $.ajax({ 
            type: "POST", 
            url: "sort_skill_be.php", 
            data: { skill: this.value }, 
            error: function (qXHR, textStatus, errorThrown) { 
                console.log(errorThrown); 
                //alert(errorThrown);
            }, 
            success: function (result) { 
                alert(result[0].skill);  // CAN I ACCESS IT LIKE THIS?
            } 
        }); 
    }); 

This is what I have so far, but I keep getting weird errors. When I added datatype:json I keep getting problems. So I feel I am handling this incorrectly, Thank you for the help!

AustinT
  • 1,998
  • 8
  • 40
  • 63
  • Are you actually specifying `application/json` (or whatever the cool kids use) as the content type? – cHao Jun 21 '13 at 01:41
  • What error did you get? – xdazz Jun 21 '13 at 01:41
  • I am getting a syntax error when I have dataType json, please refer to my previous post, http://stackoverflow.com/questions/17226224/can-anyone-help-me-catch-a-syntax-error-in-jquery – AustinT Jun 21 '13 at 01:43
  • @cHao how do I specify it? thanks for the reply guys – AustinT Jun 21 '13 at 01:43
  • @AustinTruong: In PHP? `header("Content-Type: application/json");` before you generate any output. – cHao Jun 21 '13 at 02:06
  • Where are you outputting the json? – Musa Jun 21 '13 at 02:18
  • Another big difference is you will want to echo out the content as opposed to returning it – Orangepill Jun 21 '13 at 02:18
  • Okay thanks guys, I will make some changes when I get the chance to! – AustinT Jun 21 '13 at 02:23
  • 1
    have you echo the json_encode array in sort_skill_be.php? – 蒋艾伦 Jun 21 '13 at 02:52
  • You should be able to access it the way you have it as long as result contains the indexes you try to display, along with the suggestions by @cHao , you should also declare the content type in the jQuery ajax request by adding `contentType: "application/json",` after `url: "sort_skill_be.php",` – segFault Jun 21 '13 at 04:54

0 Answers0