DOM XML Registration
<style type="text/css"> .flex_wrap {display:flex;flex-flow:row wrap;} .sel_control {margin:1rem;border:1px solid #000;border-radius:0.25rem;padding:0.25rem;} .btn_control {margin:1rem;border:1px solid #000;border-radius:0.25rem;padding:0.25rem 0.5rem;background-color:#000;color:#fff;} #user_message {margin:1rem;border:1px solid #000;border-radius:0.25rem;padding:0.25rem 0.5rem;min-height:5rem;} </style>
<div class="flex_wrap"> <input type="text" id="txtUsername" name="txtUsername" class="sel_control" minlength="8" maxlength="20" placeholder="Username" /> <input type="password" id="txtPassword" name="txtPassword" class="sel_control" minlength="8" maxlength="20" placeholder="Password" /> <input type="button" id="btnLogin" value="Register" class="btn_control" /><br/> </div> <div id="user_message"></div>
<script> window.addEventListener("DOMContentLoaded", (event) =>{ let txtUsername = document.getElementById("txtUsername"); let txtPassword = document.getElementById("txtPassword"); let btnLogin = document.getElementById("btnLogin"); let user_message = document.getElementById("user_message"); btnLogin.addEventListener("click", function() { let error_str = validate_login(); let data = ""; let msg = ""; let accessLevel = -1; let xmlhttp; let return_obj; if (error_str == "") { // PACKAGE THE DATA data = "txtUsername="+ txtUsername.value +"&txtPassword="+ txtPassword.value; if (window.XMLHttpRequest) { xmlhttp = new XMLHttpRequest(); } else { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); } xmlhttp.onreadystatechange=function() { if (xmlhttp.readyState==4 && xmlhttp.status==200) { /* 'errorString' => $error_str, 'userName' => $txtUsername, 'userId' => $largest_id, 'accessLevel' => 2); */ returnObj = JSON.parse(xmlhttp.responseText); if (returnObj.errorString != "") { msg = "Error: "+ returnObj.errorString; } else { msg += returnObj.userName +"<br/>ID: "+ returnObj.userId +"<br/>"; accessLevel = parseInt(returnObj.accessLevel); switch (accessLevel) { case 0: msg += "Registered as Adminitrator"; break; case 1: msg += "Registered as Manager"; break; case 2: msg += "Registered as User"; break; default: msg += "not registered"; } } user_message.innerHTML = msg; } } xmlhttp.open("POST","register_user.php",true); xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded"); xmlhttp.send(data); } else { user_message.innerHTML = error_str } },false); let validate_login = function() { let error_str = ""; if (txtUsername.value.trim() == "") { error_str += "You must enter a username<br/>"; } else { if (txtUsername.value.length < 8) { error_str += "Your username must be at least 8 characters long<br/>"; } } if (txtPassword.value.trim() == "") { error_str += "You must enter a password<br/>"; } else { if (txtPassword.value.length < 8) { error_str += "Your password must be at least 8 characters long<br/>"; } } return error_str; }; }); </script>
<?php // GET THE POSTED DATA - reg_add.php $txtUsername = $_POST['txtUsername']; $txtPassword = $_POST['txtPassword']; $xml_file = 'site_users_dom_1.xml'; // SET OUTPUT STRING TO EMPTY $error_str = ''; $counter = 0; /* * users * user user_id status access * user_name * user_pass */ if (file_exists($xml_file)) { $current_id = -1; $new_id = 0; $domDoc = new DOMDocument(); // CREATE A DOM DOCUMENT TO LOAD XML FILE DATA INTO $domDoc->preserveWhiteSpace = false; // SET PRESERVE WHITESPACE TO FALSE $domDoc->formatOutput = true; // MAKE OUTPUT FORMATTED WITH INDENTED CODE $domDoc->load($xml_file); // LOAD THE FILE DATA INTO THE DOM DOCUMENT $xpath = new DOMXPath($domDoc); // CREATE A NEW DOMXPath OBJECT FROM THE XML DOCUMENT $theDoc = $domDoc->documentElement; /* LOOP THROUGH ALL OF THE user NODES */ foreach($domDoc->getElementsByTagName('user') as $user) { $current_id = $user->getAttribute('user_id'); if ($current_id > $new_id) { $new_id = $current_id; } if ($user->getElementsByTagName('user_name')->item(0)->nodeValue == $txtUsername) { $error_str = 'Error: Username Already Exists'; } } if ($error_str == "") { $new_id++; $user = $domDoc->createElement('user'); $user->setAttribute( 'user_id', $new_id ); $user->setAttribute( 'status', '1' ); // ACTIVE $user->setAttribute( 'access', '2' ); // BASIC NON USER - TO BE MANUALLY EDITED $user_name = $domDoc->createElement('user_name', $txtUsername); $user_pass = $domDoc->createElement('user_pass'); // USER_PASS $user_pass->appendChild($domDoc->createCDataSection(password_hash($txtPassword, PASSWORD_DEFAULT))); // APPEND USER_NAME TO USER $user->appendChild( $user_name ); // APPEND USER_PASS TO USER $user->appendChild( $user_pass ); // APPEND USER TO USERS $theDoc->appendChild( $user ); // SAVE IN FILE "xml_users.xml" $domDoc->save($xml_file); } } else { $new_id = 0; /* FILE DOES NOT EXIST, SO CREATE XML TO BE SAVED TO FILE */ $domDoc = new DomDocument('1.0', 'UTF-8'); $domDoc->preserveWhiteSpace = false; $domDoc->formatOutput = true; // ADD ROOT ELEMENT $users = $domDoc->appendChild($domDoc->createElement('users')); // CREATE USER NODE $user = $domDoc->createElement('user'); $user->setAttribute( 'user_id', '0' ); // FIRST USER $user->setAttribute( 'status', '1' ); // ACTIVE $user->setAttribute( 'access', '2' ); // BASIC NON USER - TO BE MANUALLY EDITED $user_name = $domDoc->createElement('user_name', $txtUsername); $user_pass = $domDoc->createElement('user_pass'); // USER_PASS $user_pass->appendChild($domDoc->createCDataSection(password_hash($txtPassword, PASSWORD_DEFAULT))); // APPEND USER_NAME TO USER $user->appendChild( $user_name ); // APPEND USER_PASS TO USER $user->appendChild( $user_pass ); // APPEND USER TO USERS $users->appendChild( $user ); // SAVE IN FILE "xml_users.xml" $domDoc->save($xml_file); } $output = array('errorString' => $error_str, 'userName' => $txtUsername, 'userId' => $new_id, 'accessLevel' => 2); header('Content-Type:application/json'); echo json_encode($output, JSON_FORCE_OBJECT); ?>