0

My goal is to get the relevant rows from the database using getideas.php, so then on displayideas.php, I can actually pick them apart (e.g. get the idea with the highest score, display the idea score, display the idea in a particular html part of the code within a span).

On getideas.php, I have the following:

$sql = "SELECT evals.idea_score, evals.ideas_idea_id, ideas.idea
    FROM evals, ideas
    WHERE evals.ideas_idea_id = ideas.idea_id
        AND evals.users_eval_id = '$eval_id'";

    $result = mysql_query($sql);

    $data = array();

    while($row = mysql_fetch_assoc($result)){
        $data[] = $row["idea_score"];
        array_push($data, $row["ideas_idea_id"], $row["idea"]);
    }

    return $data;

On displayideas.php, I have the following:

$.ajax({type: "POST",
    url: "getideas.php",
    data: { eval_id: eval_id },
    success: function(response){
        console.log(response);
    }
})

I am not sure if I necessarily need an array. I am new to PHP/MySQL. I have been searching an answer to this online but cannot find a place that just puts it all together in a way I can understand to solve my problem. At this point, I feel like I have spent way too much time on it, and it is time to ask for help!

  • possible duplicate of [Send array with Ajax to PHP script](http://stackoverflow.com/questions/9001526/send-array-with-ajax-to-php-script) – Fabricator Jul 22 '14 at 06:42
  • Question is actually on how to retrieve (and then manipulate) data (versus sending). – user2802273 Jul 23 '14 at 05:23

2 Answers2

0

You need to json encode your array before using it into javascript

so use

return json_encode($data);

reference: http://in3.php.net/manual/en/function.json-encode.php

  • Tried that. Still am unable to retrieve pieces of data though... (i.e. console.log(response[1].idea) returns 'undefined'. – user2802273 Jul 23 '14 at 05:19
0

I hope this will help you. getideas.php will bocome as follow.

      $sql = "SELECT evals.idea_score, evals.ideas_idea_id, ideas.idea
FROM evals, ideas
WHERE evals.ideas_idea_id = ideas.idea_id
    AND evals.users_eval_id = '". mysql_real_escape_string($_POST['eval_id'])."'";

$result = mysql_query($sql);

$data = array();

while($row = mysql_fetch_assoc($result)){
    $data[] = $row["idea_score"];
    array_push($data, $row["ideas_idea_id"], $row["idea"]);
}

$post = json_encode($data);
    echo $post;

On displayideas.php,

       $.ajax({type: "POST",
url: "getideas.php",
dataType: "json",
data: { eval_id: eval_id },
success: function(response){
    console.log(response);//you can access data from here by response[0].attributename.ie,response[1].idea_score.
}});
Rashid
  • 13
  • 4
  • So console.log(response) actually gives me data: ["3", "324529", "idea number 1", "7", "589400", "idea number 2", "5", "862765", "idea number 3"] However, when I try console.log(response[1].idea_score), for example, I get 'undefined'... – user2802273 Jul 23 '14 at 05:17