This question already has an answer here:
I want the system to check whether the email exists in the xml file when a new registration occurs, how can I do that? I got the basic idea of comparing all the email nodes but still cant get it to work after several hours. Thanks for the help
register.html
<!DOCTYPE html>
<html lang="en">
<head>
<title>test</title>
</head>
<body>
<h1>ShipOnline System Registration Page</h1>
<form id="regform" method="post" action="register.php">
<fieldset id="person">
<legend>Your details:</legend>
<p><label for="fname">First Name:</label><input type="text" name="fname"/></p>
<p><label for="lname">Last Name:</label><input type="text" name="lname"/></p>
<p><label for="password">Password:</label><input type="password" name="password"/></p>
<p><label for="cpassword">Confirm Password:</label><input type="password" name="cpassword"/></p>
<p><label for="email">Email:</label><input type="email" id="email" name="email"/></p>
<p><label for="phone">Phone:</label><input type="text" name="phone"/></p>
<input type="submit" value="Register"/>
</fieldset>
</form>
</body>
</html>
relevant parts of register.php
<?php
$email = @trim($_POST["email"]);
if ($password != $cpassword)
{
echo 'Password does not match';
}
@override
else if ($email /*exists*/)
{
echo 'Email exists';
}
?>
customer.xml
<?xml version="1.0"?>
<customer>
<user>
<id>0</id>
<fname>John</fname>
<lname>Smith</lname>
<email>jsmith@gmail.com</email>
<password>jsmith</password>
<phone>0412345677</phone>
</user>
<user>
<id>1</id>
<fname>Arthur</fname>
<lname>Luo</lname>
<email>aluo@gmail.com</email>
<password>aluo</password>
<phone>0412345678</phone>
</user>
</div>
The easiest way is to do a simple search of the contents of your XML file. This is fine if you are confident that there are not other <email>
tags containing the email address in some other context. For example:
# slurp the XML file into a variable
$xml = file_get_contents('customer.xml');
# search for the email address within the tags
if (strpos($xml, "<email>$email</email>") !== false)) {
# you've found it!
}
You can also use DOMDocument and DOMXPath to parse the XML and search for the email address. To be honest, this seems like overkill given the simplicity of the XML file you've posted.