使用 JavaScript 函数时遇到问题

我正在获取用户输入并将它们存储在 XML 文件中。我正在读取一个 XML 文件并将所有数据存储在 JavaScript 数组中的标签下。这样,如果用户两次输入相同的电子邮件地址,我可以让他们知道该电子邮件已存在于 XML 文件中。这样它就不会在 XML 文件中存储相同的电子邮件两次,因为用户无法注册。但由于某种原因,该功能无法正常工作。我真的需要一些帮助来解决这个问题。我的 XML 文件(storedata.xml):


<?xml version="1.0" encoding="UTF-8"?>

<document>

<data><fname>Sammy</fname><lname>Kim</lname><email2>kim2000@gmail.com</email2><pass2>123</pass2></data>

<data><fname>Jimmy</fname><lname>Jabs</lname><email2>jim@gmail.com</email2><pass2>abc</pass2></data>

</document>

我的PHP代码:


<?php

if(isset($_REQUEST['ok'])) {

    $xml = new DOMDocument("1.0","UTF-8");

    $xml->load("storedata.xml");

    

    $rootTag = $xml->getElementsByTagName("document")->item(0);

    

    $dataTag= $xml->createElement("data");

    

    $name1= $xml->createElement("fname",$_REQUEST['fname']);

    $name2= $xml->createElement("lname",$_REQUEST['lname']);

    $emailadd= $xml->createElement("email2",$_REQUEST['email2']);

    $pwd= $xml->createElement("pass2",$_REQUEST['pass2']);

    

    $dataTag->appendChild($name1);

    $dataTag->appendChild($name2);

    $dataTag->appendChild($emailadd);

    $dataTag->appendChild($pwd);

    

    $rootTag->appendChild($dataTag);

    

    $xml->save("storedata.xml");    

}

?>

我的表单输入:


<input type="email" name="email1" placeholder="Email Adress" required="required" class="input-txt" style="width:40%;" id="email1">

<input type="email" name="email2" placeholder="Confirm Email Adress" required="required" class="input-txt" style="width:40%;"id="email2"> 

我的 JavaScript 代码:


<script>


var first_email = document.getElementById("email1")

  , second_email = document.getElementById("email2")

  , password = document.getElementById("password")

  , confirm_password = document.getElementById("confirm_password");

 

隔江千里
浏览 84回答 1
1回答

烙印99

你可以:existingEmails检索电子邮件后创建一个数组。然后在checkEmailExists函数中您将检查电子邮件是否在数组中。<script>var first_email = document.getElementById("email1")&nbsp; , second_email = document.getElementById("email2")&nbsp; , password = document.getElementById("password")&nbsp; , confirm_password = document.getElementById("confirm_password");&nbsp;//making an array of the email addresses from the XML filevar request = new XMLHttpRequest();request.open("GET", "storedata.xml", false);request.send();var xml = request.responseXML;var users = xml.getElementsByTagName("data");const existingEmails = [];for(var i = 0; i < users.length; i++) {&nbsp; &nbsp; const emailTag = users[i].getElementsByTagName("email2");&nbsp; &nbsp; const email = emailTag[0].childNodes[0].nodeValue;&nbsp; &nbsp; existingEmails.push(email);}//checking if the email already existsfunction checkEmailExists() {&nbsp; &nbsp; if (existingEmails.includes(first_email.value)) {&nbsp; &nbsp; &nbsp; &nbsp; first_email.setCustomValidity("This email address already exists! Please use another one.");&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; first_email.setCustomValidity('');&nbsp; &nbsp; }}email1.onchange = checkEmailExists;function validateMail() {&nbsp; &nbsp; if(first_email.value != second_email.value) {&nbsp; &nbsp; &nbsp; &nbsp; email2.setCustomValidity("Emails Don't Match");&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; email2.setCustomValidity('');&nbsp; &nbsp; }}email2.onkeyup = validateMail;
打开App,查看更多内容
随时随地看视频慕课网APP