Niruban Nicholas Niruban Nicholas - 2 months ago 14
PHP Question

Error with submitting username and password for user registration

I'm new to PHP but I have some experience with HTML and JavaScript. This is my webpage, it's a simple register screen.

<!DOCTYPE html>
<html>
<head>
<style>
.error{color: #FF0000;}
</style>
</head>
<body>
<?php
$name = $password = $confirmPassword = $email = $phone = "";
$nameErr = $passwordErr = $confirmPasswordErr = $emailErr = $phoneErr = "";

if($_SERVER["REQUEST_METHOD"] == "POST"){
//echo "insdie if";
if(empty($_POST["name"])){
$nameErr = "Name is required";
}else {
$name = $_POST["name"];
if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
$nameErr = "Only letters and white space allowed";
}
}
if($_POST["password"]){
$nameErr = "password is required";
if(!$_POST["confirmPassword"]){
$confirmPasswordErr = "Confirm password doesn't match password.";
}
}else {
$password = $_POST["password"];
if($password < 8){
$passwordErr = "Password should contain more than 8 characters";
}else
{
if(empty($_POST["confirmPassword"])){
$confirmPasswordErr = "Confirm password and password dont match";
}else {
$confirmPassword = $_POST["confirmPassword"];
if($confirmPassword != $password){
$confirmPasswordErr = "Confirm password and password dont match";
}
}
}

}
if(empty($_POST["email"])){
$nameErr = "Email is required";
}else {
$email = test_input($_POST["email"]);
}
echo "".$name."";

function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
}
?>
<form method="POST" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<h1>Register to CabsOnline</h1>
<p>Please fill th fields below to complete your registration</p>
<table>
<tr>
<td>Name:</td>
<td><input type="text" name="name"/><span class="error"><?php $nameErr;?></span></td>
</tr>
<tr>
<td>Password:</td>
<td><input type="password" name="password"/><span class="error"><?php $passwordErr;?></span></td>
</tr>
<tr>
<td>Confirm Password:</td>
<td><input type="password" name="confirmPassword"/><span class="error"><?php $confirmPasswordErr;?></span></td>
</tr>
<tr>
<td>Emal:</td>
<td><input type="text" name="email"/><span class="error"><?php $emailErr;?></span></td>
</tr>
<tr>
<td>phone:</td>
<td><input type="text" name="phone"/><span class="error"><?php $phoneErr;?></span></td>
</tr>
</table>

<br/>

<input type="submit" value="Register"/>
<br/>

<h4>Already registered?</h4><a href="login.php">Loging here</a>
</form>
</body>




The code doesn't add the validation and I can't find out why. What do I have to do to get the value from the textboxes only when submit is clicked? I've changed it around but I just can't find an answer.

Answer

You need to actually echo the strings.

<?php $nameErr;?>

on its own doesn't do anything, it should be

<?php echo $nameErr;?>