473,815 Members | 3,993 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

An AJAX Simple Example for PHP

An AJAX Simple Example for PHP
Article from http://www.joyistar.com
Introduction:
AJAX WebShop 3 Beta2 supports PHP programming by integrating PHP5
development environment. Here we will give an simple example to show
you how to develop web applications by PHP in AJAX WebShop.

1, Notice
We will use Access database in this example. Of course, you can use
others you like. Database name: demo.mdb; Table name: product; Fields:
PRODUCT_ID, PRODUCT_NAME, PRODUCT_PRICE and PRODUCT_AREA.

2, Example
Start AJAX WebShop 3 to new a project by "File->New Project"
Give a project name in the popup window and select the web server as
PHP.
New a PHP Service by "File->New .PHP Service" and input the
required content. Select the "ServiceTyp e" as "Query Data".
Click "OK", WebShop will generate codes automatically.
Input the codes below(Notice: demo.mdb should be the same directory
with the source code files.):
$connstr = "DRIVER=Microso ft Access Driver
(*.mdb);DBQ=".r ealpath($_SERVE R['PATH_TRANSLATE D'])."\demo.mdb" ;
$sql = "select * from product";
$sqlcount = "select count(*) from product";

The source code of testquery.php£º
1. Fill data to $xmlRequest in columns:
//fill metadata fields
for ($i=1; $i<=$fields_cou nt; $i++) {
$fieldname = odbc_field_name ($result_id, $i);
$datatype = odbc_field_type ($result_id, $i);
$xmlRequest->addField($fiel dname, $datatype);
}

2. Fill data to $xmlRequest in rows£º
//fill data rows
for ($i=0; $i<$record_coun t; $i++) {
odbc_fetch_row( $result_id);
if($i>=$recNo && $i<$recNo+$maxR ows) {
$xmlRequest->append();
for ($j=1; $j<=$fields_cou nt; $j++) {
$xmlRequest->setValueByInde x($j-1, odbc_result($re sult_id, $j));
}
}
if($i>=$recNo+$ maxRows) break;
}
3£® Get the paged parameters, $xmlRequest->recNo is the first record,
$xmlRequest->maxRows is the total records number in a page and to get
the total records number by executing "select count(*) from
product"£º
$sqlcount = "select count(*) from product";
$result_id = @odbc_do($conni d, $sqlcount);
if($result_id== null)
throw new Exception($sqlc ount);
odbc_fetch_row( $result_id);
$record_count = odbc_result($re sult_id,1);
$xmlRequest->setRecordCount ($record_count) ;
$recNo = $xmlRequest->recNo;
$maxRows = $xmlRequest->maxRows;
if($maxRows==-1) $maxRows = $record_count;
After the steps above, we need a web page to display the result. Select
"File-New Page" and fill the file name.
Drag these components to the page from "Data Access" and
"DBControl" . These components include "DataSet",
"DBNavigato r", "DBEdit" and "DbGrid". Select the component
to edit it's properties.

DataSet1: Set "OpenURL" as "demo/simple_query.ph p" and Start
WebServer by "Run->Start WebServer". The Apache HttpServer will
start. Add fields in "Fields" by click the "..." to pop up the
Field window and right click to "Refresh" to get the fields name
and select all, click "Ok". Set the Active as "True".

Set the "DataSet" as "DataSet1" in other components to finish
the data binding with database. Be attention that you need to set the
DataField of DBEdit.

Click "Run" to view the result.
Appendix: Source codes:
<?php
/**
* Title:
* Description:
* Copyright: Copyright (c) 2006
*
* $Author:
* $Date:
* $Revision:
*/
header("Content-Type:text/xml");
require_once('. ./joyistar/XmlRequest.php' );
$postdata = file_get_conten ts("php://input");
$xmlRequest = new XmlRequest();
$xmlRequest->open($postdata );
try {
$connstr = "DRIVER=Microso ft Access Driver
(*.mdb);DBQ=".r ealpath($_SERVE R['PATH_TRANSLATE D'])."\demo.mdb" ;
$sql = "select * from product";
$connid = @odbc_connect($ connstr,"","",S QL_CUR_USE_ODBC ) or die
("db connect error!");

$sqlcount = "select count(*) from product";
$result_id = @odbc_do($conni d, $sqlcount);
if($result_id== null)
throw new Exception($sqlc ount);
odbc_fetch_row( $result_id);
$record_count = odbc_result($re sult_id,1);
$xmlRequest->setRecordCount ($record_count) ;
$recNo = $xmlRequest->recNo;
$maxRows = $xmlRequest->maxRows;
if($maxRows==-1) $maxRows = $record_count;

$result_id = @odbc_do($conni d, $sql);
if($result_id== null)
throw new Exception($sql) ;
$fields_count = odbc_num_fields ($result_id);
//fill metadata fields
for ($i=1; $i<=$fields_cou nt; $i++) {
$fieldname = odbc_field_name ($result_id, $i);
$datatype = odbc_field_type ($result_id, $i);
$xmlRequest->addField($fiel dname, $datatype);
}
//fill data rows
for ($i=0; $i<$record_coun t; $i++) {
odbc_fetch_row( $result_id);
if($i>=$recNo && $i<$recNo+$maxR ows) {
$xmlRequest->append();
for ($j=1; $j<=$fields_cou nt; $j++) {
$xmlRequest->setValueByInde x($j-1, odbc_result($re sult_id, $j));
}
}
if($i>=$recNo+$ maxRows) break;
}
odbc_close($con nid);

echo $xmlRequest->getXml();
}
catch (Exception $e) {
$xmlRequest->setError($e->getTraceAsStri ng());
echo $xmlRequest->getXml();
throw $e;
}
?>

Jan 9 '07 #1
0 1823

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
1865
by: Alex | last post by:
Example uploaded to: http://www.clickatus.com/ajax/ BTW - This is for FIREFOX, won't work in IE. I don't know why but when it is executed the browser still in loading state... Even though XMLHttpRequest already got its string from the server... Any help?
7
3776
by: Ivan Marsh | last post by:
Hey Folks, I'm having a heck of a time wrapping mind around AJAX. Anyone know of a simple, straight-forward example for pulling a simple query from mysql with PHP using AJAX? As I understand it I need a PHP script that pulls the query and dumps the data into XML format, that is called by triggering a javascript event that reads that file with XMLhttprequest.
31
3175
by: Tony | last post by:
I just noticed that prototype.js is one of the files in the Ajax.NET distribution - I'm pretty concerned about this. Does anyone know if this is the same "prototype.js" that is not well-liked around here? If so, do you know if Ajax.NET can be used without prototype.js? -- "The most convoluted explanation that fits all of the made-up facts is the most likely to be believed by conspiracy theorists. Fitting the
1
16517
by: www.web20developers.com | last post by:
http://www.web20developers.com http://www.web20developers.com/index.php?option=com_content&task=view... Ajallerix : AJAX, simple, fast Web image gallery demo ; at Novell AJAX - microlink pattern tutorial : A microlink is a link that opens up
10
4421
by: celoftis | last post by:
Using VS 2005: This morning, following the instructions here: http://ajax.asp.net/docs/overview/InstallingASPNETAJAX.aspx, I installed ASP.NET AJAX on my WinXP Pro development machine. Following that, I installed the "November 2006 CTP" and "Control Toolkit" - then I re-booted my machine. Next I tried to create a simple, sample page... I have the AJAX controls in my toolbox, but when I drag on onto my page it gets underlined - denoting...
4
3854
by: Nathan Sokalski | last post by:
I am a beginner with AJAX, and have managed to learn how to use it when passing single parameters, but I want to return more than one value to the client-side JavaScript function that displays it. My client-side JavaScript function takes 4 parameters (which are expected to be integers). The idea of passing a single parameter and parsing it on the client has occurred to me, but since I am sure I am not the only person who has situations that...
25
2806
by: meltedown | last post by:
This is supposed ot be an example: http://www.ajaxtutorial.net/index.php/2006/11/30/simple-ajax-using-prototype-part-2/ It says : This example is probably the simplest example you will ever find. We are going to use the prototype feature ‘ajax.Updater’ (see part one for more details on prototype).
1
4036
by: geevaa | last post by:
http://www.phpbuilder.com/columns/kassemi20050606.php3 XMLHttpRequest and AJAX for PHP programmers James Kassemi Introduction: Although the concept isn't entirely new, XMLHttpRequest technology is implemented on more sites now than ever. Compatibility is no longer an issue (IE, Mozilla and Opera all support it), and the benefits to using it are amazing. There are too many PHP programmers avoiding any
17
11888
by: Arjen | last post by:
Hi, I want to reload 2 divs at one click. Ive tried: <a href = "javascript:void(0);" onclick="show('ajaxrequest.php?action=removefield','div1');show('ajaxrequest.php?action=reloaddiv2','div2')">verwijderen</a> While both seperate actions work they dont when I put them together. Anyone know how to fix this ? My ajax.js with funcition show
0
9610
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10670
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10426
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10142
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9225
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7686
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6897
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5570
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
3
3030
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.