0

I have posted a question about How to alert a php array in ajax success function in this post: How to alert a php array in ajax success function

I got the answer to use alert(JSON.stringify(result[0])); but this will give me access to the first row of the array while i need to have access to each element of each row. imagine that my array is like this now

 [0]1>>>>2>>>>>3>>>>>5>>>>>6
 [1]1>>>>2>>>>>3>>>>>5>>>>>6
 [2]1>>>>2>>>>>3>>>>>5>>>>>6
 [3]1>>>>2>>>>>3>>>>>5>>>>>6
 [4]1>>>>2>>>>>3>>>>>5>>>>>6
 [5]1>>>>2>>>>>3>>>>>5>>>>>6

alert(JSON.stringify(result[0])) will give me only [0]1>>>>2>>>>>3>>>>>5>>>>>6 but I want to alert 5 only, I have tried alert(JSON.stringify(result[0][3])) but no luck.

Could you tell me how to have access to 2d array elements using JSON.stringify? Or is there any other way than JSON.stringify(result[0]

Here is the ajax function :

  $.ajax({
  type: 'POST',
  url: "profile/ajax/getorder.php",
  data: {id:gotid},
  dataType: 'json',
  cache: false,
  success: function(result) {
  alert(JSON.stringify(result[0]))

      },
  });

here is the php

$ent = $_POST['id'];
$column = array();
$gtord = mysql_query("SELECT * FROM order WHERE oId = '$ent' ");

while($rowmnu2=mysql_fetch_array($gtord))
{
 $column[] =  $rowmnu2;             
}
echo json_encode($column);

Appreciated.

1 Answer 1

0

The json.stringify method simply gets the array in a text format that can be sent to alert. If you are only looking at a single value that isn't an array, you don't need it. Just alert(result[0][3]).

After getting more info:

To access a whole row, use JSON.stringify(result[0]), to look at a specific element use result[0]['user_id']. If you want to use numbers to look at the specific elements, use mysql_fetch_row instead of mysql_fetch_assoc. Hope that helps.

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

10 Comments

thanks but that alerts undefined :( here is how i get the result while($rowmnu2=mysql_fetch_assoc($gtord)) { $column[] = array($rowmnu2); } echo json_encode($column);
Are you sure that the getorder.php file is really giving you a two dimensional array? It seems to me that it is a single dimensional array. Perhaps you should check that.
Can you post what is printed on the page if you go to "profile/ajax/getorder.php" directly, I would guess json is not formated right.
are you trying to sent all of those values to alert, or just one element, like the user_id?
also you seem to have an extra [ at the start of your second element, this means that you might be parsing the data incorrectly, and the later elements are more deeply nested then they should be. It looks like your structure is {{}{{}{{}{{}}}} instead of {{}{}{}}{{}{}{}}{{}{}{}}
|

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.