如何将新数据添加到XML中

时间:2017-04-11 14:28:35

标签: javascript php ajax xml

register.htm

<HTML XMLns="http://www.w3.org/1999/xHTML">
<head>      
    <link rel="stylesheet" type="text/css" href="mystyle.css" />
    <script type="text/javascript" src="shoponline.js"></script> 
    <title>ShopOnline Register Page</title>
</head>

<body>
    <H3>ShopOnline</H3>     

    <table width="100%">
        <tr>
            <td><button class="button" name="btnHome" value="Home" onclick="location.href='login.htm';">Home</button></td>
            <td><button class="button" name="btnListing" value="Listing" onclick="location.href='listing.htm';">Listing</button></td>
            <td><button class="button" name="btnBidding" value="Bidding" onclick="location.href='bidding.htm';">Bidding</button></td>
            <td><button class="button" name="btnMaintenance" value="Maintenance" onclick="location.href='maintenance.htm';">Maintenance</button></td>
            <td><button class="button" name="btnLogout" value="Logout" onclick="location.href='#';">Log Out</button></td>
        </tr>
    </table>

    <hr><br><br>

    <I>To register a new account with ShipOnline, please complete the registration form below.</I>

    <br><br>

    <form>
        <div class="form">
            <fieldset>
                <legend>Registration Details</legend>
                <p style="text-align:left;">* Required Fields</p>
                <br>
                <table>
                    <tr>
                        <td style="text-align:right;">First Name <label>*</label></td>
                        <td><input type="text" id="txtFName" name="txtFName" class="textbox" placeholder="Enter Your First Name"
                            oninvalid="this.setCustomValidity('Please Enter Your First Name!')" oninput="setCustomValidity('')" required /></td>
                    </tr>
                    <tr>
                        <td style="text-align:right;">Surname <label>*</label></td>
                        <td><input type="text" id="txtSurname" name="txtSurname" class="textbox" placeholder="Enter Your Surname"
                            oninvalid="this.setCustomValidity('Please Enter Your Surname!')" oninput="setCustomValidity('')" required /></td>
                    </tr>
                    <tr>
                        <td style="text-align:right;">Email <label>*</label></td>
                        <td><input type="email" id="txtEmail" name="txtEmail" class="textbox" placeholder="Enter Your Email Address"
                            oninvalid="this.setCustomValidity('Please Enter Your Email!')" oninput="setCustomValidity('')" required /></td>
                    </tr>
                    <tr>
                        <td></td>
                        <td>
                            <input type="button" name="btnRegister" value="Register" onclick="register()" />
                            <input type="button" name="btnReset" value="Reset" onclick="reset('registerForm')" />
                        </td>
                    </tr>
                </table>
            </fieldset>
        </div>  
    <form>

    <div id="information"></div>

</body>
</HTML>

shoponline.js

var xHRObject = false;

if (window.XMLHttpRequest)
    xHRObject = new XMLHttpRequest();
else if (window.ActiveXObject)
    xHRObject = new ActiveXObject("Microsoft.XMLHTTP");


function reset(formID) {
    switch (formID) {
        case 'loginForm':
            document.getElementById('txtEmail').value = "";
            document.getElementById('txtPassword').value = "";
            break;
        case 'registerForm':
            document.getElementById('txtFName').value = "";
            document.getElementById('txtSurname').value = "";
            document.getElementById('txtEmail').value = "";
            break;
    }
}

function login() {
    var email = document.getElementById('txtEmail').value;
    var pass = document.getElementById('txtPassword').value;

    xHRObject.open("GET", "login.php?id=" + Number(new Date) + "&email=" + email + "&pass=" + pass, false); 
    xHRObject.send(); 

    if (xHRObject.responseText == true)
        document.location.href = "bidding.htm";
    else
        window.alert("Login Failure!");
}

function register() {
    var isFound = false;
    var email = document.getElementById('txtEmail').value;
    var firstName = document.getElementById('txtFName').value;
    var surName = document.getElementById('txtSurname').value;

    xHRObject.open("GET", "register.php?id=" + Number(new Date) + "&email=" + email + "&firstName=" + firstName + "&surName=" + surName, false);
    xHRObject.send();

if (xHRObject.responseText == true)
    window.alert("Registration Failure!");
else
    window.alert("Registration Success!");
}

customer.xml     

<customers>
<customer>
    <ID>C1</ID>
    <FirstName>Jack</FirstName>
    <SurName>Wong</SurName>
    <Email>jack@hotmail.com</Email>
    <Password>081292</Password> 
</customer> 

<customer>
    <ID>C2</ID>
    <FirstName>Ashley</FirstName>
    <SurName>Rachael</SurName>
    <Email>ashley@hotmail.com</Email>
    <Password>081292</Password> 
</customer>

<customer>
    <ID>C3</ID>
    <FirstName>Vongola</FirstName>
    <SurName>Steve</SurName>
    <Email>vongola@hotmail.com</Email>
    <Password>081292</Password> 
</customer>
</customers>

register.php

<?php
$xmlFile = "customer.xml";
$isFound = false;
$count = 0;

// Check the XML is exist or not. 
// If does, do checking against the email address.
if (file_exists($xmlFile)) {
    $dom = new DOMDocument();
    $dom->load($xmlFile);
    $customer = $dom->getElementsByTagName("customer"); 

    foreach($customer as $node) { 
        $fName = $node->getElementsByTagName("FirstName");
        $valueFName = $fName->item(0)->nodeValue;

        $email = $node->getElementsByTagName("Email");
        $valueEmail = $email->item(0)->nodeValue;   

        if($_GET["email"] == $valueEmail)
            $GLOBALS["isFound"] = true;

        $GLOBALS["count"]++;
    }

    if ($isFound != true) {
        // UNIQUE EMAIL, START UPDATE XML & SEND MAIL
        $firstName = $_GET["firstName"];
        $surName = $_GET["surName"];
        $email = $_GET["email"];
        $password = random_password(8);

        $xml = new SimpleXMLElement($xmlFile);

        $customer = $xml->addChild("customer");
        $customer->addChild("ID", "C" .($count + 1));
        $customer->addChild("FirstName", $firstName);
        $customer->addChild("SurName", $surName);
        $customer->addChild("Email", $email);
        $customer->addChild("Password", $password);

        $xml->saveXML($xmlFile);
    }

} else {
    // XML FILES NOT EXIST. STRAIGHT AWAY CREATE A NEW ONE AND SEND EMAIL
}

ECHO ($isFound);

?>

基本上,我使用AJAX进行服务器和客户端通信。 因此,当最终用户在register.htm完成输入他们的信息时,它将使用XMLHttpsRequest.open转到javascript文件到register.php来更新XML数据。 但它并没有更新我的XML数据。 任何建议将不胜感激。

0 个答案:

没有答案