Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/282.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何将新数据添加到XML中_Javascript_Php_Ajax_Xml - Fatal编程技术网

Javascript 如何将新数据添加到XML中

Javascript 如何将新数据添加到XML中,javascript,php,ajax,xml,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>Sh

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>
customer.xml


C1
杰克
黄
jack@hotmail.com
081292
C2
艾希礼
瑞秋
ashley@hotmail.com
081292
C3
旺格拉
史蒂夫
vongola@hotmail.com
081292
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数据。
任何建议都将不胜感激。

saveXML
需要一个文件名
如果指定,函数将数据写入文件而不是返回它
saveXML
asXML
的别名,我也尝试过。但我的xml文件也没有更新。P/S$xmlFile=“customer.xml”您的代码工作正常-xml更新部分正常,我已经测试过了。因此,漏洞在别处。缺少写入权限?奇怪。我应该在这里发布整个代码吗@@
saveXML
需要文件名
如果指定,函数将数据写入文件,而不是返回它
saveXML
asXML
的别名,我也尝试过。但我的xml文件也没有更新。P/S$xmlFile=“customer.xml”您的代码工作正常-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>
<?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);

?>