通过PHP保存到XML

时间:2014-10-10 14:50:57

标签: javascript php xml

好吧我可以做这个%100错了我遇到麻烦PHP到XML我只是想保存客户的详细信息.......(不是试图保证安全而只是学习如何做到这一点)

Html是一种简单的形式 用于姓名,姓氏,密码,电子邮件和可选的电话号码(目前不需要)

PHP代码是在javascript调用javascript后启动的。

PHP

<?php
            // header('Content-Type: text/xml');
                session_register('Customer');
                //session_start('customer'); 
            $doc = new DOMDocument();
            $doc->load('customer.xml');

            $firstname = $_GET["firstName"];
            $lastname = $_GET["lastName"];
            $email = $_GET["email"];
            $password = $_GET["password"];

            $MDA = $_SESSION["Customer"]; //assign the session varaible to MDA


            if (isset($firstname)) {
            $customerArray = array();
            $customerArray['firstname'] = $firstname;
            $customerArray['lastname'] = $lastname;
            $customerArray['email'] = $email;
            $customerArray['password'] = $password;
            $MDA[$firstname] = $customerArray;
            $_SESSION["Customer"] = $MDA;
                ECHO (toXml($MDA)); 
            }


            function toXml($MDA)
            {                    
                $doc = new DomDocument('1.0');
                $Customer = $doc->createElement('Customers');
                $Customer = $doc->appendChild($Customer);
                $root = $doc->appendChild($doc->createElement('Root'));

                foreach ($MDA as $a => $b)
                {
                    $nodeA = $doc->createElement('NodeA');
                    $root->appendChild($nodeA);
                    $nodeA->appendChild($attr1);
                    $attr1 = $doc->createAttribute('firstname');
                    $attr1->appendChild($doc->createTextNode($a['firstname']));

                $attr = $doc->createElement('firstname');
                $attr = $Customer->appendChild($person);

                $lastname = $doc->createElement('lastname'); 
                $lastname = $person->appendChild($lastname);   
                $value = $doc->createTextNode($a);
                $value = $lastname->appendChild($value);

                $email = $doc->createElement('email');
                $email = $person->appendChild($email);
                $value2 = $doc->createTextNode($b['email']);
                $value2 = $email->appendChild($value2);

                $password = $doc->createElement('password');
                $password = $person->appendChild($password);
                $value3 = $doc->createTextNode($b['password']);
                $value3 = $password->appendChild($value3);        
              }

                $doc->formatOutput = true; 
                $strXml = $doc->saveXML();
                //$doc->save('customer.xml'); 
                return $strXml;
            }
        ?>

并且不确定它是否需要但是javascript ...它只是检查密码atm

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


        function test() 
        {
            var firstname = document.getElementById("firstName").value;
            var lastname = document.getElementById("lastName").value;
            var email = document.getElementById("email").value;
            var password = document.getElementById("password").value;
            var password2 = document.getElementById("password2").value;
            var number = document.getElementById("pNumber").value;
            var type = "";
            var input = document.getElementsByTagName("input");
              xHRObject.open("GET", "testregristation.php?", true);
              xHRObject.onreadystatechange = function() {
                   if (xHRObject.readyState == 4 && xHRObject.status == 200)
                    if (password != password2) {
                    alert("Password is wrong");

                    }
                    else
                    {
                        alert("test " +firstname + " " +  lastname + " " + email + " : " + password);
                       document.getElementById('information').innerHTML = xHRObject.responseText;
              }
              xHRObject.send(); 
        };
        }

1 个答案:

答案 0 :(得分:0)

过时浏览器问题,这在Firefox中运行良好