0

How can I handle my json object resulted from an Ajax call in PHP file instead of js file (I am using jquery)?

Let's say I have a phonebook table with name, phonenum columns. My index file have an input text field and when someone types something and clicks submit button, an Ajax call will be sent to server. The server will return an json object contains all matching phone numbers. So, how can I convert (or decode) this object into PHP variable and pass it to PHP script (in order to display phone numbers in a table, for example).

EDIT

I dont mean 'get json from server, then send it back to the server'. In fact, I mean "convert the json object to 'something' like a PHP variable, and I can draw a table of phone numbers on client browser". Maybe I need something like 'jquery template'. I want to receive data in Json format instead draw a table on the server, then send back as HTML because I want to reduce server processes. Can you give me any advice for my problem? something like 'jquery template' which can understand json object as same as PHP understand PHP object $obj?

3
  • Why are you wanting to get data from the PHP server in JSON format, then send it back to the server, so the server can then give you it in another format? Why the extra, unnecessary step? Don't forget that PHP is only run on the server, before the page itself is even served! PHP and JavaScript on a page aren't running at the same time. Commented Feb 16, 2012 at 18:22
  • "Passing to PHP" would require an aditional Ajax call. Why don't you have your original call return the HTML, instead of JSON? Or build the HTML from javascript using the JSON. Commented Feb 16, 2012 at 18:22
  • @Kitsune and bfavaretto: thanks for your reminders. In fact I mean "convert the json object to 'something' like a PHP variable, and I can draw a table of phone numbers on client browser". Maybe I need something like 'jquery template'. I want to receive data in Json format instead draw a table on the server, then send back as HTML because I want to reduce server processes. Can you give me any advice for my problem? something like 'jquery template' which can understand json object as same as PHP understand PHP object $obj? Commented Feb 16, 2012 at 18:45

3 Answers 3

1

JSON is just a method of serializing data structures. It's language agnostic so if you serialize something with json in javascript you can unserialize the result with php and have the same object.

PHP Manual References:

json_encode()

json_decode()

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

2 Comments

json object present in jquery ajax success function, how can i do json.decode from php.
how can i pass json object into php script to decode or is there any other way to pass this json values into php script as array?
0

You could create a template using JQuery's .tmpl() function. The page documents the format that JQuery expects, and you can even do substitution on it.

Comments

0

If you're using jQuery and jQuery is receiving the results of the ajax call, you'll probably want to handle it in jQuery and insert the results into your HTML with jQuery since JSON is just a javascript object literal and requires no additional parsing.

If you want to turn json text into a php array, you can use json_decode in php.

1 Comment

json object present in jquery ajax success function, how can i do json.decode from php.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.