Devon.Marcus1 Devon.Marcus1 - 1 month ago 10
HTML Question

variables not being read in if statement in php

I'm trying to generate a form, posting to an xml doc but the code keeps taking the else option "Please make sure you filled in the fields correctly." in the registration.php file when I've supplied the variables properly.
There's 3 sections: html, javascript and php. The output I'm getting is

<html>
<head>
<title>A Simple Example</title>
</head>
<body>
<script src="clientSideScripts.js"></script>
<h1>Registration Page </h1>
<table border="0">
<form>
<tr>
<td><strong>Firstname:</strong></td>
<td>
<input type="text" name="firstname" id="firstname" required />
</td>
</tr>
<tr>
<td><strong>Lastname:</strong></td>
<td>
<input type="text" name="lastname" id="lastname" required />
</td>
</tr>
<tr>
<td><strong>Password:</strong></td>
<td>
<input type="password" id="userPassword" name="password" required/>
</td>
</tr>
<tr>
<td><strong>Confirm Password:</strong></td>
<td>
<input type="password" id="confirmPassword" name="pwdfield" required/>
</td>
</tr>
<tr>
<td><strong>Email:</strong></td>
<td>
<input type="email" name="email" id="email" value="" required pattern="[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}$" />
</td>
</tr>
<tr>
<td><strong>Contact Phone:</strong></td>
<td>
<input type="text" name="phone" id="phone" value=""/>
</td>
</tr>
<td>
<input type="button" value="Register" onclick="registerCustomer();"/>
</td>
</form>
<div id="information"/>
</body>
</html>


var xHRObject = false;

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

function validatePassword()
{
var a = document.getElementById("userPassword");
var b=document.getElementById("confirmPassword");
return a.value == b.value;
}

function registerCustomer()
{
if(!validatePassword())
alert("The passwords don't match.");
else
{
var firstname = document.getElementById("firstname").value;
var lastname = document.getElementById("lastname").value;
var password = document.getElementById("userPassword").value;
var email = document.getElementById("email").value;
var phone = document.getElementById("phone").value;
var url = "registration.php?firstname=" + firstname + "&lastname" + lastname + "&password=" + password + "&email=" + email + "&phone=" + phone;
xHRObject.open("GET", url , true);
xHRObject.onreadystatechange = function()
{
if (xHRObject.readyState == 4 && xHRObject.status == 200)
document.getElementById('information').innerHTML = xHRObject.responseText + "<br><a href='buyonline.htm'>back</a>";
}
xHRObject.send(null);
}
}


function customerLogin()
{
var email = document.getElementById("email").value;
var password = document.getElementById("password").value;
var url = "login.php?email=" + email + "&password=" + password;
xHRObject.open("GET", url , true);
xHRObject.onreadystatechange = function()
{
if (xHRObject.readyState == 4 && xHRObject.status == 200)
document.getElementById('information').innerHTML = xHRObject.responseText + "<br><a href='buyonline.htm'>back</a>";
}
xHRObject.send(null);
}

<?php
if(isset($_GET["firstname"]) && isset($_GET["lastname"]) && isset($_GET["password"]) && isset($_GET["email"]))
{
if(isEmailUnique() )
insertCustomer();
else
echo "This email is already taken, please choose another one.";
}
else echo "Please make sure you filled in the fields correctly.";

function getLastId(){
$id = 0;
$xmlFile = "../../data/customer.xml";
try
{
$dom = DOMDocument::load($xmlFile);
$customers = $dom->getElementsByTagName("customer");

foreach($customers as $node)
{
$cID = $node->getElementsByTagName("id");
$cID = $cID->item(0)->nodeValue;
if (($id < $cID )) $id = $cID;
}
}
catch(Exception $e)
{
$doc = new DomDocument('1.0');
$customers = $doc->createElement('customers');
$customers = $doc->appendChild($customers);

$doc->saveXML();
return 1;
}
return $id;
}

function insertCustomer()
{
try {
$xmlFile = "../../data/customer.xml";
$doc = DOMDocument::load($xmlFile);
$doc->formatOutput = true;
$customer = $doc->createElement("customer");

$Customers = $doc->getElementsByTagName("Customers");


$customer = $Customers->item(0)->appendChild($customer);
$newID = getLastId() + 1;
$id = $doc->createElement('id');
$idValue = $doc->createTextNode($newID);
$id->appendChild($idValue);
$customer->appendChild($id);


$name1 = $doc->createElement('firstname');
$nameValue = $doc->createTextNode($_GET["firstname"]);
$value2 = $name1->appendChild($nameValue);
$name = $customer->appendChild($name1);

$name = $doc->createElement('lastname');
$nameValue = $doc->createTextNode($_GET["lastname"]);
$value2 = $name->appendChild($nameValue);
$name = $customer->appendChild($name);

$name = $doc->createElement('password');
$nameValue = $doc->createTextNode($_GET["password"]);
$value2 = $name->appendChild($nameValue);
$name = $customer->appendChild($name);


$name = $doc->createElement('email');
$nameValue = $doc->createTextNode($_GET["email"]);
$value2 = $name->appendChild($nameValue);
$name = $customer->appendChild($name);

$name = $doc->createElement('phone');
$nameValue = $doc->createTextNode($_GET["phone"]);
$value2 = $name->appendChild($nameValue);
$name = $customer->appendChild($name);

echo "<xmp>".$doc->save($xmlFile)."</xmp>";

}
catch(Exception $e) { echo $e;}

echo "customer successfully registered and your new Id = ". $newID;
}


function isEmailUnique()
{
$xmlFile = "../../data/customer.xml";
try
{
$dom = DOMDocument::load($xmlFile);
$customers = $dom->getElementsByTagName("customer");

foreach($customers as $node)
{
$email = $node->getElementsByTagName("email");
$email = $email->item(0)->nodeValue;

if (($email == $_GET["email"])) return false;
}
}
catch(Exception $e)
{
$doc = new DomDocument('1.0');
$customers = $doc->createElement('customers');
$customers = $doc->appendChild($customers);

$doc->saveXML();
return true;
}
return true;
}
?>

Answer

I think you're missing a key equals sign here (inside registerCustomer() function):

"...firstname=" + firstname + "&lastname" + lastname + "&password="

Contrast with:

"...firstname=" + firstname + "&lastname=" + lastname + "&password="

This means the GET index lastname is never being set.