Json Data string from database recordsWe will be sending data back to the main page by using Json formatted strings. These strings will contain number of Jason data value pairs taken from database table along with some other data.
Getting a single recordLet us first try by collecting and returning a single record from our student table. For better security we have used PDO to connect to MySQL data base and getting the records. We have not discussed the mysql data collection part here, only the data formatting is shown.
In above code we have used json_encode function to generate the json string. Here is the output or the Json string
Along with single data row we will also try to send few more data . We are sending two arrays of data here. There are two members one is data and other one is value. Each member contains two arrays containing some objects.
Array of recordsWe will generate json string from an array of data records. Here is the code to get data and return the Json string
Here is the Json string as output
Receiving dataIn above code we have kept one array of data ( array name is data ) and some single data. Now we will learn how to receive these data and display them.
Inside your Ajax function first let us try to collect the whole bunch of data.
We know the array name is data. So we will loop through the array to display each row of record of the array.
In addition to these array of records we have some single records stored in value. Here one sample to retrieve them
Similarly another one
Displaying data or calling from a file.Different scripts can call any file which displays data in Json string. For example we can keep all the code discussed here inside one PHP file and give the name as
To display this data we can include it like this from our main script.
Or we can call this page by using jQuery
We can also use Ajax to call function
To see the example on how this code is used, you can read our Ajax based PHP MySQL Paging script
Number of User Comments : 1
Ajax & PHP scripts Creating XMLHttp object in different browsers Sample Code: Get Method of Ajax form submission Server Clock:: Display real-time date and time of server Dependant drop down list box using Ajax & PHP Email validation using Ajax in a form Getting customer details by entering customer id using Ajax Progress Bar using Ajax Progress Bar using MySQL PHP & Ajax Displaying Message at client side using Ajax & PHP Web page HTML form validation using Ajax and PHP Form validation with onBlur event using Ajax and PHP
Json support in PHP json_encode to generate Json string from PHP Array data Json Data formatting to return database records to main script Searching MySql database as we type using Ajax Displaying all records based on selection of a drop down list box