Niruban Nicholas Niruban Nicholas - 3 months ago 13
HTML Question

Error with submitting php code

Im new to php but i have some experience with html and javascript. This is my webpage, its 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 doesnt add the validation and i cant find out why. Can someone please explain what i have to do to get the value from the textbox's only when submit is clicked? i've changed it around but i just cant find an answer.

Thanks.

Answer

You need to actually echo the strings.

<?php $nameErr;?>

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

<?php echo $nameErr;?>