0

I am making a simple web app, in one part of it, the result of SQL query is to be passed from PHP to JavaScript with AJAX.

This is the SQL Query:

$meta_query = mysql_fetch_row(mysql_query("SELECT * from meta WHERE user_id='$user_id'"));

This is how I pass it to JavaScript var_dump($meta_query);

This is what I am getting at JavaScript (as a string):

array(30) {[0]=>string(1) "3"["id"]=>string(1) "3"[1]=>string(2) "14"["user_id"]=>string(2) "14"[2]=>string(10) "29-06-2014"["date"]=>string(10) "29-06-2014"[3]=>string(1) "0"["present"]=>string(1) "0"[4]=>string(1) "0"["future"]=>string(1) "0"}

How, how do I convert this into a proper JavaScript object so that the output is something like:

{"id":"3","user_id":"14","date":"29-06-2014","Present":0,"Future":0}

How do I convert data from the first format to the second in JavaScript? Or should I do something else entirely in PHP to get the data out in some other format? Or should I convert the data to the required format somehow in PHP?

2 Answers 2

2

You might want to use

echo json_encode($meta_query);
Sign up to request clarification or add additional context in comments.

1 Comment

Then afterwards in JavaScript use JSON.parse.
1

from php side toy should convert php array to json object using
<?php echo json_encode($array)
here is tutorial how to do this

Don't forget to put in your jquery request dataType: "json"

Comments

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.