6 Replies - 605 Views - Last Post: 27 October 2013 - 08:26 PM

#1 jerryh218  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 27-October 13

AJAX registration form question

Posted 27 October 2013 - 03:48 AM

I am trying to create a registration form, using AJAX.

My assignment specifies that I must use HTML, Javascript, PHP and XML to do so, but I'm having trouble linking them all together.

My code is supposed to generate an XML file if none currently exists, and if the XML exists, it should append to the file.

I can only get this to work if I stick the php code directly into my HTML and change my register.htm to register.php, but even then my email check is not working. I should only be able to insert a new customer if the email address is currently not in the XML document.

Any help or advise would be appreciated! Thanks! :)/>

HTML:
<!DOCTYPE html>
<HTML>
	<head> 
	<title>Registration Form</title>
		<script type="text/javascript" src="validation.js"></script>
		<script type="text/javascript" src="register.js"></script>
	</head>

	<body>
		<h1>Registration Form</h1>
		<div id="registrationForm">
		<form name="myForm" id="myForm" action="register.htm" onsubmit="return(validate());" method="post">
			<table>
			<tr>
				<td align="right"><label>* First Name:</label></td>
				<td align="left"><input type="text" id="firstname" name="firstname" maxlength="100" style="height: 20px; width: 150px"></td>
			</tr>
					<tr>
				<td align="right"><label>* Last Name:</label></td>
				<td align="left"><input type="text" id="lastname" name="lastname" maxlength="100" style="height: 20px; width: 150px"></td>
			</tr>
			<tr>
				<td align="right"><label>* Email:</label></td>
				<td align="left"><input type="text" id="email" name="email" maxlength="100" style="height: 20px; width: 150px"></td>
			</tr>
			<tr>
				<td align="right"><label>* Password:</label></td>
				<td align="left"><input type="password" id="password1" name="password1" maxlength="25" style="height: 20px; width: 150px"></td>
			</tr>
					<tr>
				<td align="right"><label>* Repeat Password:</label></td>
				<td align="left"><input type="password" id="password2" name="password2" maxlength="25" style="height: 20px; width: 150px"></td>
			</tr>
			<tr>
				<td align="right"><label>Phone:</label></td>
				<td align="right">( <input type="text" id="phone1" name="phone1" maxlength="2" style="height: 20px; width: 20px"> )
				<input type="text" id="phone2" name="phone2" maxlength="8" style="height: 20px; width: 86px"></td>
			</tr>
			<tr>
				<td> </td>
				<td align="right"><input type="submit" value="Register" style="height: 25px; width: 80px" onclick="retrieveRegistrationInfo()"/></td>
			</tr>
			<tr>
				<td alright="left">* Required fields</td>
				<td></td>
			</tr>
			</table>
		</form>
		<span id="status"></span>
		</div>
	</body>
</HTML>



PHP:
<?php
{
	$xmlFile = "../../data/customer.xml";
	$uid = uniqid();

	$firstname = $_POST['firstname'];
	$lastname = $_POST['lastname'];
	$email = $_POST['email'];
	$password = $_POST['password1'];
	$phone = $_POST['phone1'].$_POST['phone2'];

if (file_exists($xmlFile))
	{

		$xml = new DOMDocument();
		$xml->preserveWhiteSpace = false;
		$xml->loadXML(file_get_contents($xmlFile));
		$xml->formatOutput = true;

		$emailaddress = $xml->getElementsByTagName("email");
		
		for ($i = 0; $i < count($xml); $i++)
		{
			$checkemail = $emailaddress->item($i)->nodeValue;

			if ($checkemail == $email)
			{
				echo "Email Aready Exists! Please choose another!" ;
			}
			else
			{
				$customers = $xml->getElementsByTagName("customers");

				$xml_customer = $xml->createElement("customer");
				$xml_customer_id = $xml->createElement("customer_id");
				$xml_name = $xml->createElement("name");
				$xml_firstname = $xml->createElement("firstname");
				$xml_lastname = $xml->createElement("lastname");
				$xml_email = $xml->createElement("email");
				$xml_password = $xml->createElement("password");
				$xml_phone = $xml->createElement("phone");

				$xml_firstname->appendChild($xml->createTextNode($firstname));
				$xml_lastname->appendChild($xml->createTextNode($lastname));

				$xml_name->appendChild($xml_firstname);
				$xml_name->appendChild($xml_lastname);

				$xml_customer_id->appendChild($xml->createTextNode($uid));
				$xml_email->appendChild($xml->createTextNode($email));
				$xml_password->appendChild($xml->createTextNode(md5($password)));
				$xml_phone->appendChild($xml->createTextNode($phone));

				$xml_customer->appendChild($xml_customer_id);
				$xml_customer->appendChild($xml_name);
				$xml_customer->appendChild($xml_email);
				$xml_customer->appendChild($xml_password);
				$xml_customer->appendChild($xml_phone);
				
				$customers->item(0)->appendChild($xml_customer);

				$xml -> save($xmlFile);
				echo "You have successfully registered with Buy Online! Click <a href='buyonline.htm'> here </a> to go back to the main page!";
			}
		}
	}
else
	{
		$xml = new DOMDocument();
		$xml->formatOutput = true;
		
		$xml_customers = $xml->createElement("customers");
		$xml_customer = $xml->createElement("customer");
		$xml_customer_id = $xml->createElement("customer_id");
		$xml_name = $xml->createElement("name");
		$xml_firstname = $xml->createElement("firstname");
		$xml_lastname = $xml->createElement("lastname");
		$xml_email = $xml->createElement("email");
		$xml_password = $xml->createElement("password");
		$xml_phone = $xml->createElement("phone");


		$xml_firstname->appendChild($xml->createTextNode($firstname));
		$xml_lastname->appendChild($xml->createTextNode($lastname));

		$xml_name->appendChild($xml_firstname);
		$xml_name->appendChild($xml_lastname);

		$xml_customer_id->appendChild($xml->createTextNode($uid));
		$xml_email->appendChild($xml->createTextNode($email));
		$xml_password->appendChild($xml->createTextNode($password));
		$xml_phone->appendChild($xml->createTextNode($phone));

		$xml_customer->appendChild($xml_customer_id);
		$xml_customer->appendChild($xml_name);
		$xml_customer->appendChild($xml_email);
		$xml_customer->appendChild($xml_password);
		$xml_customer->appendChild($xml_phone);
		$xml_customers->appendChild( $xml_customer);
		$xml->appendChild($xml_customers);

		$xml->save($xmlFile);

		echo "You have successfully registered with Buy Online! Click <a href='buyonline.htm'> here </a> to go back to the main page!";
	}

}
?>



Javascript:
var xhr = false;

if (window.ActiveXObject)
{
	xhr = new ActiveXObject("Microsoft.XMLHTTP");
}
else if (window.XMLHttpRequest)
{
	xhr = new XMLHttpRequest();
}

function retrieveRegistrationInfo() 
{
	
	
	xhr.onreadystatechange=function()
   {
   if (xhr.readyState==4 && xhr.status==200)
     {
     document.getElementById("status").innerHTML=xmlhttp.responseText;
     }
   }


	//url to connect to
	var url = "register.php";
	// declaring that the data being sent is in XML format
	//xhr.setRequestHeader("Content-Type", "text/xml");
	xhr.open("POST", url, true);
	// Send the request
	xhr.send();

}



Is This A Good Question/Topic? 0
  • +

Replies To: AJAX registration form question

#2 laytonsdad  Icon User is offline

  • Cheese and Sprinkles
  • member icon

Reputation: 439
  • View blog
  • Posts: 1,865
  • Joined: 30-April 10

Re: AJAX registration form question

Posted 27 October 2013 - 10:44 AM

To get your ajax to work you will need to change xmlhttp to xhr on line 20 of your code above.

This post has been edited by laytonsdad: 27 October 2013 - 10:50 AM

Was This Post Helpful? 0
  • +
  • -

#3 jerryh218  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 27-October 13

Re: AJAX registration form question

Posted 27 October 2013 - 02:55 PM

Ah thanks! can't believe I didn't spot that!

but it's still not working :(
Was This Post Helpful? 0
  • +
  • -

#4 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3459
  • View blog
  • Posts: 11,717
  • Joined: 12-December 12

Re: AJAX registration form question

Posted 27 October 2013 - 03:10 PM

for ($i = 0; $i < count($xml); $i++)

I assume you intend to iterate the email-collection $emailaddress, rather than count($xml). I haven't checked what count($xml) would return, if anything. Perhaps the entire length of the file.

And each time within this loop you are writing and saving to a file. Is this your intention.

Quote

but it's still not working
This isn't helpful. What is it doing, or not doing? Do you receive any errors? Check the browser's console for errors as well.

This post has been edited by andrewsw: 27 October 2013 - 03:11 PM

Was This Post Helpful? 0
  • +
  • -

#5 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3459
  • View blog
  • Posts: 11,717
  • Joined: 12-December 12

Re: AJAX registration form question

Posted 27 October 2013 - 03:20 PM

I haven't deciphered this entirely, but it looks to me like you are using ajax to post data, rather than using the form, but you aren't actually posting any data. But, as I say, I have explored in any detail.
Was This Post Helpful? 0
  • +
  • -

#6 jerryh218  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 27-October 13

Re: AJAX registration form question

Posted 27 October 2013 - 07:48 PM

View Postandrewsw, on 27 October 2013 - 03:10 PM, said:

This isn't helpful. What is it doing, or not doing? Do you receive any errors? Check the browser's console for errors as well.



I've changed $xml to $emailaddress instead, and at the moment, I can add duplicate email addresses UNLESS the email address is the first of the collection in the XML file.
Was This Post Helpful? 0
  • +
  • -

#7 jerryh218  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 27-October 13

Re: AJAX registration form question

Posted 27 October 2013 - 08:26 PM

I've changed my PHP code to this, but it is still doing the same thing. I can add the registered user to the XML file as long as the email address is not the same as the first one on the XML file. I've added a boolean variable "$emailexists" and setting it to true if an identical email is found, but it is not iterating through the lists of emails for some reason...

<!DOCTYPE html>
<HTML>
	<head> 
	<title>Registration Form</title>
		<script type="text/javascript" src="validation.js"></script>
		<script type="text/javascript" src="register.js"></script>
	</head>

	<body>
		<h1>Registration Form</h1>
		<div id="registrationForm">
		<form name="myForm" id="myForm" action="register2.php" onsubmit="return(validate());" method="post">
			<table>
			<tr>
				<td align="right"><label>* First Name:</label></td>
				<td align="left"><input type="text" id="firstname" name="firstname" maxlength="100" style="height: 20px; width: 150px"></td>
			</tr>
					<tr>
				<td align="right"><label>* Last Name:</label></td>
				<td align="left"><input type="text" id="lastname" name="lastname" maxlength="100" style="height: 20px; width: 150px"></td>
			</tr>
			<tr>
				<td align="right"><label>* Email:</label></td>
				<td align="left"><input type="text" id="email" name="email" maxlength="100" style="height: 20px; width: 150px"></td>
			</tr>
			<tr>
				<td align="right"><label>* Password:</label></td>
				<td align="left"><input type="password" id="password1" name="password1" maxlength="25" style="height: 20px; width: 150px"></td>
			</tr>
					<tr>
				<td align="right"><label>* Repeat Password:</label></td>
				<td align="left"><input type="password" id="password2" name="password2" maxlength="25" style="height: 20px; width: 150px"></td>
			</tr>
			<tr>
				<td align="right"><label>Phone:</label></td>
				<td align="right">( <input type="text" id="phone1" name="phone1" maxlength="2" style="height: 20px; width: 20px"> )
				<input type="text" id="phone2" name="phone2" maxlength="8" style="height: 20px; width: 86px"></td>
			</tr>
			<tr>
				<td> </td>
				<td align="right"><input type="submit" value="Register" style="height: 25px; width: 80px" onclick="retrieveRegistrationInfo()"/></td>
			</tr>
			<tr>
				<td alright="left">* Required fields</td>
				<td></td>
			</tr>
			</table>
		</form>
		<span id="status"></span>
		</div>
	</body>
</HTML>

<?php

if (isset($_POST['firstname']) && isset($_POST['lastname']))
{
	$xmlFile = "../../data/customer.xml";
	$uid = uniqid();

	$firstname = $_POST['firstname'];
	$lastname = $_POST['lastname'];
	$email = $_POST['email'];
	$password = $_POST['password1'];
	$phone = $_POST['phone1'].$_POST['phone2'];

if (file_exists($xmlFile))
	{

		$xml = new DOMDocument();
		$xml->preserveWhiteSpace = false;
		$xml->loadXML(file_get_contents($xmlFile));
		$xml->formatOutput = true;
		$emailexists = false;

		$emailaddress = $xml->getElementsByTagName("email");
		
		for ($i = 0; $i < count($emailaddress); $i++)
		{
			$checkemail = $emailaddress->item($i)->nodeValue;

			if ($checkemail == $email)
				$emailexists = true;
			else
				$emailexists = false;

		}
	
		if ($emailexists == true)
			{
				echo "The email you entered already exists!";
			}
			else
			{
				$customers = $xml->getElementsByTagName("customers");

				$xml_customer = $xml->createElement("customer");
				$xml_customer_id = $xml->createElement("customer_id");
				$xml_name = $xml->createElement("name");
				$xml_firstname = $xml->createElement("firstname");
				$xml_lastname = $xml->createElement("lastname");
				$xml_email = $xml->createElement("email");
				$xml_password = $xml->createElement("password");
				$xml_phone = $xml->createElement("phone");

				$xml_firstname->appendChild($xml->createTextNode($firstname));
				$xml_lastname->appendChild($xml->createTextNode($lastname));

				$xml_name->appendChild($xml_firstname);
				$xml_name->appendChild($xml_lastname);

				$xml_customer_id->appendChild($xml->createTextNode($uid));
				$xml_email->appendChild($xml->createTextNode($email));
				$xml_password->appendChild($xml->createTextNode(md5($password)));
				$xml_phone->appendChild($xml->createTextNode($phone));

				$xml_customer->appendChild($xml_customer_id);
				$xml_customer->appendChild($xml_name);
				$xml_customer->appendChild($xml_email);
				$xml_customer->appendChild($xml_password);
				$xml_customer->appendChild($xml_phone);
				
				$customers->item(0)->appendChild($xml_customer);

				$xml -> save($xmlFile);
				echo "You have successfully registered with Buy Online! Click <a href='buyonline.htm'> here </a> to go back to the main page!";
			}
		}
else
	{
		$xml = new DOMDocument();
		$xml->formatOutput = true;
		
		$xml_customers = $xml->createElement("customers");
		$xml_customer = $xml->createElement("customer");
		$xml_customer_id = $xml->createElement("customer_id");
		$xml_name = $xml->createElement("name");
		$xml_firstname = $xml->createElement("firstname");
		$xml_lastname = $xml->createElement("lastname");
		$xml_email = $xml->createElement("email");
		$xml_password = $xml->createElement("password");
		$xml_phone = $xml->createElement("phone");


		$xml_firstname->appendChild($xml->createTextNode($firstname));
		$xml_lastname->appendChild($xml->createTextNode($lastname));

		$xml_name->appendChild($xml_firstname);
		$xml_name->appendChild($xml_lastname);

		$xml_customer_id->appendChild($xml->createTextNode($uid));
		$xml_email->appendChild($xml->createTextNode($email));
		$xml_password->appendChild($xml->createTextNode($password));
		$xml_phone->appendChild($xml->createTextNode($phone));

		$xml_customer->appendChild($xml_customer_id);
		$xml_customer->appendChild($xml_name);
		$xml_customer->appendChild($xml_email);
		$xml_customer->appendChild($xml_password);
		$xml_customer->appendChild($xml_phone);
		$xml_customers->appendChild( $xml_customer);
		$xml->appendChild($xml_customers);

		$xml->save($xmlFile);

		echo "You have successfully registered with Buy Online! Click <a href='buyonline.htm'> here </a> to go back to the main page!";
	}

}
?>


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1