Re: Read file

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



It's usually easier to show with an example : This took about 30 minutes to write :

Here's an XML file - copy and paste, and save as hotels.xml :

<?xml version="1.0"?>
<hotels>
        <hotel>
                <id>1</id>
                <name>Bangkok oriental</name>
                <address>50 High Street, Kowloon</address>
                <phone>+12 451 708 3282</phone>
        </hotel>
        <hotel>
                <id>2</id>
                <name>Hilton Marriott</name>
                <address>2042 Michael Wood Street</address>
                <phone>+44 238 9125467</phone>
        </hotel>
        <hotel>
                <id>3</id>
                <name>Swallow Royal</name>
                <address>91-97 Royal Fort Crescent</address>
                <phone>+01 1209 432 2345</phone>
        </hotel>
</hotels>

At 10:21 19/11/2003 +0000, you wrote:

Hi Neil,

thank you so much for your advice! i have not programmed XML before so it
will take me a while to learn it.. your suggestion sounds really fantastic,
expecially the part when i don't even have to ask the users to direct me to
the file that i need..

my problem, simply put will be:
1) user has to enter details in a form
2) when user enters HotelCode in one of the fields in the  form,
3) javascript must pass the value to PHP to search for the HotelName,
HotelAddress, HotelTel etc..

Forget about PHP if you are doing this client side, you only need it to process the updated records :


>5) javascript place those values in the respective fields
6) user continues to complete the form

Here is a processing page which will populate the form using the XML data : Save this in the same directory you placed the hotels.xml file; It will work in IE5 as is :


<html>
<head>
        <title>Demo Hotel Loader</title>
</head>

<body onload="populatelist()">
<script language="javascript1.2">
<!--
oxmlDocument = new ActiveXObject("MSXML2.DOMDocument");
oxmlDocument.load("hotels.xml");

function populatelist() {
        var hotels=oxmlDocument.selectNodes("/hotels/hotel");
        if (hotels != null) {
                d=document.forms["hotelupdate"].list;
                d.length=0;
                d[0]=new Option('--Please Select--','',true);
                for (i=0;i<hotels.length;i++) {
                        d.length++;
                        d[i+1]=new Option();
                        d[i+1].value=hotels[i].selectSingleNode("id").text;
                        d[i+1].text=hotels[i].selectSingleNode("name").text;
                }
        }
}

function populatevalues() {
        d=document.forms["hotelupdate"];
// Get hotel ID #
        v=d.list[d.list.selectedIndex].value;
// Use this to read the hotel/phone node having this hotel ID
        field=oxmlDocument.selectSingleNode("/hotels/hotel[id='"+v+"']/phone");
// Set form's phone field to the text of this node
        d.phone.value=field.text;
// Now to read the hotel/address node having this hotel ID
        field=oxmlDocument.selectSingleNode("/hotels/hotel[id='"+v+"']/address");
// Set form's address field to the text of this node
        d.address.value=field.text;
}
//-->
</script>

<form name="hotelupdate" id="hotelupdate" action="post" method="http://www.my-server.com/myurl.php"; onsubmit="alert('This would send these values to your website');return false;">
<select name="list" id="list" onchange="populatevalues()"></select> Phone :
<input type="text" name="phone" id="phone" size="15" maxlength="24" />
<br />
<textarea name="address" id="address" rows="3" cols="40" wrap="virtual"></textarea>
<br />
<input type="submit" name="submit" id="submit" value="Send !" />
</form>


</body>
</html>

You then need to write some PHP to accept the new record (you might give the ID number from your database as the primary key). Writing out XML from PHP works exactly the same as writing out HTML. You just fill in the blanks with your data by running a query and printing out the values between the tags, in a loop.

Hope that helps - study the code and see if you can understand how it works.
Cheers - Neil.

--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [PHP Users]     [Postgresql Discussion]     [Kernel Newbies]     [Postgresql]     [Yosemite News]

  Powered by Linux