SQL PHP HTML ASP JavaScript articles and free scripts to download
 

Data formatting by using database data and strings

We 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.

We will create data value pairs and post them back to main script. Here is the way we generate data formats for main script to use.

We have not discussed the mysql data collection part here, only the data formatting is shown. See the while loop collecting the data from the table. Here the data coming from mysql table is formatted and after the while loop some common data like messages, last record etc are posted here.

$str= "{ \"data\" : [";

while($nt=mysql_fetch_array($t)){
$str=$str."{\"id\" : \"$nt[id]\", \"name\" : \"$nt[name]\", \"myclass\" : \"$nt[class]\", \"mark\" : \"$nt[mark]\"},"; //$str=$str."{\"myclass\" : \"$nt[class]\"},";
}
$str=substr($str,0,(strLen($str)-1));

if(($endrecord) < $nume ){$end="yes";}
else{$end="no";}

if(($endrecord) > $limit ){$startrecord="yes";}
else{$startrecord="no";}

$str=$str."],\"value\" : [{\"endrecord\" : $endrecord,\"limit\" : $limit,\"end\" : \"$end\",\"startrecord\" : \"$startrecord\",\"message\" : \"$message\"}]}";

To access the data at our receiving file we have to use JavaScript array and here is some sample code to receive the data.

var myObject = eval('(' + httpxml.responseText + ')');
var msg=myObject.value[0].message

To see the example on this code is used, you can read our Ajax based PHP MySQL Paging script



Further readings
Ajax & PHP scripts
Creating XMLHttp object in different browsers
Sample Code: Get Method of Ajax form submission
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

Post Comment This is for short comments only. Use the forum for more discussions.
Name
Email( not to be displayed)Privacy Policy
1+2=This is to prevent automatic submission by spammers. Please enter the result of the sum as asked


Join Our Email List
Email:  
For Email Newsletters you can trust
Ajax & PHP
PHP Sections