0

I am passing a multidimensional array back to a .php file using json, jquery, and ajax. My goal is essentially to fill a drop down box (id=project) with multiple entries. Here's some code snippets:

$("#turninId").change(function() {

        var user_id = $("#turninId").val();

        $.ajax ( {
          url:"send_input.php",
          type: "POST",
          dataType: "json",
          data:{id_selection: user_id},
          success:function(response) {

            for (var i=0; i<response.proj.length; i++) {
              $("#exp").html(response.proj[i]);
              $("#project").html(response.proj[i]); } });

     });

In send_input.php (backend), I query a db, and send the information to an array. Then I use json_encode.

$query="SELECT project FROM main";
$results = $db->query($query);
while ($row_id = $results->fetchArray()) {
       $proj_option[] = "<option value=\"".$row_id['project']."\">".$row_id['project']."</option>\n";
           $pselected=$row_id['project'];
}
$output = array( "proj" => "$proj_option");
echo json_encode($output);

My problem is that this is RETURNING THE STRING "Array".

For example, if I do: response.proj[0], I get "A" returned.

What gives? I've seen a few people with questions about this error, but no definite solution. Any help?

1 Answer 1

1

This is because you are casting $proj_option to a string by enclosing it in quotes. Just remove the quotes and you will get the array.

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

2 Comments

Hm, doing that does remove the "array" problem but only returns the last element of the query. Why is this?
Thanks, this was a serious problem that i had!

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.