The PHP page called by the JavaScript, is called "getuser.php".
The PHP script runs an SQL query against a MySQL database, and returns the result as HTML:
< ?php
When the query is sent from the JavaScript to the PHP page, the following happens: 1. PHP opens a connection to a MySQL server 2. The correct person is found 3. An HTML table is created, and filled with data, and sent back to the "txtHint" placeholder When a person in the drop-down box is selected, the showUser() function executes the following: 1. Calls the GetXmlHttpObject() function to create an XMLHTTP object 2. Defines an URL (filename) to send to the server 3. Adds a parameter (q) to the URL with the content of the drop-down box 4. Adds a random number to prevent the server from using a cached file 5. Each time the readyState property changes, the stateChanged() function will be executed 6. Opens the XMLHTTP object with the given URL 7. Sends an HTTP request to the server This is the JavaScript code stored in the file "selectuser.js": var xmlhttp; The stateChanged() and GetXmlHttpObject functions are the same as in the PHP AJAX Suggest chapter, you can go to there for an explanation of those. The HTML page contains a link to an external JavaScript, an HTML form, and a div element: <> As you can see it is just a simple HTML form with a drop down box called "customers". The When the user selects data, a function called "showUser()" is executed. The execution of the function is triggered by the "onchange" event. In other words: Each time the user change the value in the drop down box, the function showUser() is called. The database table we use in this example looks like this: id FirstName LastName Age Hometown Job 1 Peter 41 Quahog Brewery 2 Lois 40 Piano Teacher 3 Joseph Swanson 39 Quahog Police Officer 4 Glenn Quagmire 41 Quahog Pilot The following example will demonstrate how a web page can fetch information from a database with Person info will be listed here. The server paged called by the JavaScript, is a PHP file called "getcd.php". The PHP script loads an XML document, "cd_catalog.xml", runs a query against the XML file, and returns the result as HTML: < ?php When the CD query is sent from the JavaScript to the PHP page, the following happens: 1. PHP creates an XML DOM object 2. Find all 3. Output the album information (send to the "txtHint" placeholder) When a person in the drop-down box is selected, the showUser() function executes the following: 1. Calls the GetXmlHttpObject() function to create an XMLHTTP object 2. Defines an URL (filename) to send to the server 3. Adds a parameter (q) to the URL with the content of the drop-down box 4. Adds a random number to prevent the server from using a cached file 5. Each time the readyState property changes, the stateChanged() function will be executed 6. Opens the XMLHTTP object with the given URL 7. Sends an HTTP request to the server This is the JavaScript code stored in the file "selectcd.js": var xmlhttp The stateChanged() and GetXmlHttpObject functions are the same as in the PHP AJAX Suggest chapter, you can go to there for an explanation of those. The following example will demonstrate how a web page can fetch information from an XML file with CD info will be listed here... The HTML page contains a link to an external JavaScript, an HTML form, and a div element: <> As you can see it is just a simple HTML form with a simple drop down box called "cds". The <> below the form will be used as a placeholder for info retrieved from the web server. When the user selects data, a function called "showCD" is executed. The execution of the function is triggered by the "onchange" event. In other words: Each time the user change the value in the drop down box, the function showCD is called. The stateChanged() function executes every time the state of the XMLHTTP object changes. When the state changes to 4 ("complete"), the content of the txtHint placeholder is filled with the response text. The code in the "gethint.php" checks an array of names and returns the corresponding names to the client: < ?php If there is any text sent from the JavaScript (strlen($q) > 0), the following happens: 1. Find a name matching the characters sent from the JavaScript 2. If no match were found, set the response string to "no suggestion" 3. If one or more matching names were found, set the response string to all these names 4. The response is sent to the "txtHint" placeholder The showHint() function above calls a function named GetXmlHttpObject(). The purpose of the GetXmlHttpObject() function is to solve the problem of creating different XMLHTTP objects for different browsers. The showHint() function above is executed every time a character is entered in the "txt1" input field. If there is input in the input field (str.length > 0), the showHint() function executes the following: If the input field is empty, the function simply clears the content of the txtHint placeholder. The following Type a name in the input field below: Suggestions: The HTML page contains a link to an external JavaScript, a simple HTML form, and a span element: <> The HTML form above has an input field called "txt1". An event attribute for this field defines a function to be triggered by the onkeyup event. The paragraph below the form contains a span called "txtHint". The span is used as a placeholder for data retrieved from the web server. When a user inputs data, the function called "showHint()" is executed. The execution of the function is triggered by the "onkeyup" event. In other words: Each time a user moves the finger away from a keyboard key inside the input field, the function showHint is called. This is the JavaScript code, stored in the file "clienthint.js": var xmlhttp All new browsers use the built-in JavaScript XMLHttpRequest object to create an XMLHttpRequest object (IE5 and IE6 uses an ActiveXObject). The JavaScript code for creating an XMLHttpRequest object: if (window.XMLHttpRequest) To get or send information from/to a database or a file on the server with traditional JavaScript, you will have to make an HTML form, and a user will have to click the "Submit" button to send/get the information, wait for the server to respond, then a new page will load with the results. Because the server returns a new page each time the user submits input, traditional web applications can run slowly and tend to be less user-friendly. With With the XMLHttpRequest object, a web page can make a request to, and get a response from a web server - without reloading the page. The user will stay on the same page, and he or she will not notice that scripts request pages, or send data to a server in the background. The XMLHttpRequest object is supported in all major browsers (Internet Explorer, Firefox, Chrome, Opera, and Safari). There is no such thing as an In our PHP tutorial we will demonstrate how a web page can communicate with a PHP web server online. There is nothing new to learn. Internet-applications have many benefits over desktop applications; they can reach a larger audience, they are easier to install and support, and easier to develop. However, Internet-applications are not always as "rich" and user-friendly as traditional desktop applications. With With The Below is an XML file: < ?xml version="1.0" encoding="ISO-8859-1"? > We want to output the element names and data from the XML file above. Here's what to do: 1. Load the XML file 2. Get the name of the first element 3. Create a loop that will trigger on each child node, using the children() function 4. Output the element name and data for each child node Example < ?php The output of the code above will be: note As of PHP 5.0, the SimpleXML functions are part of the PHP core. There is no installation needed to use these functions. SimpleXML is new in PHP 5. It is an easy way of getting an element's attributes and text, if you know the XML document's layout. Compared to DOM or the Expat parser, SimpleXML just takes a few lines of code to read text data from an element. SimpleXML converts the XML document into an object, like this: SimpleXML is fast and easy to use when performing basic tasks like: However, when dealing with advanced XML, like namespaces, you are better off using the Expat parser or the XML DOM. We want to initialize the XML parser, load the XML, and loop through all elements of the < ?php The output of the code above will be: #text = In the example above you see that there are empty text nodes between each element. When XML generates, it often contains white-spaces between the nodes. The XML DOM parser treats these as ordinary elements, and if you are not aware of them, they sometimes cause problems. We want to initialize the XML parser, load the xml, and output it: < ?php The output of the code above will be: Tove Jani Reminder Don't forget me this weekend! If you select "View source" in the browser window, you will see the following HTML: < ?xml version="1.0" encoding="ISO-8859-1"? > The example above creates a DOMDocument-Object and loads the XML from "note.xml" into it. The XML file below will be used in our example: < ?xml version="1.0" encoding="ISO-8859-1"? > To read and update - create and manipulate - an XML document, you will need an XML parser. There are two basic types of XML parsers: The DOM parser is an tree-based parser. Look at the following XML document fraction: < ?xml version="1.0" encoding="ISO-8859-1"? > The XML DOM sees the XML above as a tree structure: The W3C DOM provides a standard set of objects for HTML and XML documents, and a standard interface for accessing and manipulating them. We want to initialize the XML parser in PHP, define some handlers for different XML events, and then parse the XML file. < ?php The output of the code above will be: -- Note -- How it works: 1. Initialize the XML parser with the xml_parser_create() function 2. Create functions to use with the different event handlers 3. Add the xml_set_element_handler() function to specify which function will be executed when the parser encounters the opening and closing tags 4. Add the xml_set_character_data_handler() function to specify which function will execute when the parser encounters character data 5. Parse the file "test.xml" with the xml_parse() function 6. In case of an error, add xml_error_string() function to convert an XML error to a textual description 7. Call the xml_parser_free() function to release the memory allocated with the xml_parser_create() function The XML Expat parser functions are part of the PHP core. There is no installation needed to use these functions. The XML file below will be used in our example: < ?xml version="1.0" encoding="ISO-8859-1"? > |