...

View Full Version : Validation Form PHP



PF2G
01-07-2012, 03:04 PM
Hi i have this code with the validation, it's incomplete yet. But i was testing and when i forgot something it warns me but erases all the information previously filled. What i want is that it doesn't erases.

Here's the code:

<?php

include ('database_connection.php');
if (isset($_POST['formsubmitted'])) {
$error = array();//Declare An Array to store any error message
if (empty($_POST['name'])) {//if no name has been supplied
$error[] = 'Please Enter a name ';//add to array "error"
} else {
$name = $_POST['name'];//else assign it a variable
}

if (empty($_POST['e-mail'])) {
$error[] = 'Please Enter your Email ';
} else {


if (preg_match("/^([a-zA-Z0-9])+([a-zA-Z0-9\._-])*@([a-zA-Z0-9_-])+([a-zA-Z0-9\._-]+)+$/", $_POST['e-mail'])) {
//regular expression for email validation
$Email = $_POST['e-mail'];
} else {
$error[] = 'Your EMail Address is invalid ';
}


}


if (empty($_POST['Password'])) {
$error[] = 'Please Enter Your Password ';
} else {
$Password = $_POST['Password'];
}


if (empty($error)) //send to Database if there's no error '

{ // If everything's OK...

// Make sure the email address is available:
$query_verify_email = "SELECT * FROM members WHERE Email ='$Email'";
$result_verify_email = mysqli_query($dbc, $query_verify_email);
if (!$result_verify_email) {//if the Query Failed ,similar to if($result_verify_email==false)
echo ' Database Error Occured ';
}

if (mysqli_num_rows($result_verify_email) == 0) { // IF no previous user is using this email .


// Create a unique activation code:
$activation = md5(uniqid(rand(), true));


$query_insert_user = "INSERT INTO ( `Username`, `Email`, `Password`, `Activation`) VALUES ( '$name', '$Email', '$Password', '$activation')";


$result_insert_user = mysqli_query($dbc, $query_insert_user);
if (!$result_insert_user) {
echo 'Query Failed ';
}

if (mysqli_affected_rows($dbc) == 1) { //If the Insert Query was successfull.


// Send the email:
$message = " To activate your account, please click on this link:\n\n";
$message .= WEBSITE_URL . '/activate.php?email=' . urlencode($Email) . "&key=$activation";
mail($Email, 'Registration Confirmation', $message, 'From: ismaakeel@gmail.com');

// Flush the buffered output.


// Finish the page:
echo '<div class="success">Thank you for
registering! A confirmation email
has been sent to '.$Email.' Please click on the Activation Link to Activate your account </div>';


} else { // If it did not run OK.
echo '<div class="errormsgbox">You could not be registered due to a system
error. We apologize for any
inconvenience.</div>';
}

} else { // The email address is not available.
echo '<div class="errormsgbox" >That email
address has already been registered.
</div>';
}

} else {//If the "error" array contains error msg , display them



echo '<div class="errormsgbox"> <ol>';
foreach ($error as $key => $values) {

echo ' <li>'.$values.'</li>';



}
echo '</ol></div>';

}

mysqli_close($dbc);//Close the DB Connection

} // End of the main Submit conditional.

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Registration Form</title>





<style type="text/css">
/* Box Style */


.success, .warning, .errormsgbox, .validation {
border: 1px solid;
margin: 0 auto;
padding:10px 5px 10px 50px;
background-repeat: no-repeat;
background-position: 10px center;
font-weight:bold;
width:450px;

}

.success {

color: #4F8A10;
background-color: #DFF2BF;
background-image:url('images/success.png');
}
.warning {

color: #9F6000;
background-color: #FEEFB3;
background-image: url('images/warning.png');
}
.errormsgbox {

color: #D8000C;
background-color: #FFBABA;
background-image: url('images/error.png');

}
.validation {

color: #D63301;
background-color: #FFCCBA;
background-image: url('images/error.png');
}



</style>

</head>
<body>


<form action="index.php" method="post" class="registration_form">
<fieldset>
<legend> ALUNO </legend>

<br/>
<div class="field"><label>Username*: </label><input type="text" size="30" name = "username"/></div>
<br/>
<div class="field"><label>Password*: </label><input type="password" size="25" name = "pass"/></div>
<br/>
<div class="field"><label>Confirma Password*: </label><input type="password" size="25" name = "conf_pass"/></div>
<br/>
<div class="field"><label>Nome*: </label><input type="text" size="40" name = "nome"/></div>
<br/>
<div class="field"><label>Data de Nascimento: </label><input id="txtChar" onkeypress="return isNumberKey(event)" type="text" size="3" maxlength="2" name = "dia"/> / <input id="txtChar" onkeypress="return isNumberKey(event)" type="text" size="3" maxlength="2" name = "mes"/> / <input id="txtChar" onkeypress="return isNumberKey(event)" type="text" size="5" maxlength="4" name = "ano"/> (DD/MM/AAAA)</div>
<br/>
<div class="field"><label>Morada: </label><input type="text" onkeypress="return onlyNumbers();" size="39" name = "morada"/></div>
<br/>
<div class="field"><label>Código Postal: </label><input id="txtChar" onkeypress="return isNumberKey(event)" type="text" size="5" maxlength="4" name = "cp1"/> - <input id="txtChar" onkeypress="return isNumberKey(event)" type="text" size="4" maxlength="3" name = "cp2"/></div>
<br/>
<div class="field"><label>Telemóvel*: </label><input id="txtChar" onkeypress="return isNumberKey(event)" type="text" size="10" maxlength="9" name = "tele"/></div>
<br/>
Curso*: <select name="curso">
<option value = 1> Piano </option>
<option value = 2> Orgão </option>
<option value = 3> Guitarra Elétrica </option>
<option value = 4> Guitarra Acústica </option>
<option value = 5 Violino </option>
<option value = 6> Canto </option>
<option value = 7> Bateria </option>
<option value = 8> Saxofone </option>
<option value = 9> Flauta </option>
<option value = 10> Baixo </option>
<option value = 11> Violoncelo </option>
</select>
<br/>
<br/>
<div class="field"><label>E-Mail*: </label><input type="text" value="" name = "email" size = "25"/></div>
<br/>
Observações:
<br/>
<textarea cols="30" rows="7" name="obs"></textarea>
</fieldset>
<br/>
<fieldset>
<legend> ENCARREGADO DE EDUCAÇÃO </legend>

<br/>
<div class="field"><label>Nome*: </label><input type="text" size="40" name = "nome_ee"/></div>
<br/>
<div class="field"><label>Morada: </label><input type="text" size="39" name = "morada_ee"/></div>
<br/>
<div class="field"><label>Código Postal: </label><input id="txtChar" onkeypress="return isNumberKey(event)" type="text" size="5" maxlength="4" name = "cp1_ee"/> - <input id="txtChar" onkeypress="return isNumberKey(event)" type="text" size="4" maxlength="3" name = "cp2_ee"/></div>
<br/>
<div class="field">Telemóvel*: </label><input id="txtChar" onkeypress="return isNumberKey(event)" type="text" size="10" maxlength="9" name = "tele_ee"/></div>
<br/>
<div class="field"><label>E-Mail*: </label><input type="text" value="" name = "email_ee" size = "25"/></div>

</fieldset>
<br/>
* Campos Obrigatórios
<br/>
<div class="submit">
<input type="hidden" name="formsubmitted" value="TRUE" />
<input type="submit" value="Register" />
</div>
</fieldset>
</form>

</body>
</html>


Can someone help me please?

melloorr
01-07-2012, 03:06 PM
So if you press the back button in your browser, the information goes?

PF2G
01-07-2012, 03:08 PM
So if you press the back button in your browser, the information goes?

No, i press "submit" and it goes to the form automatically with the a box with the errors, and there's when it erases everything.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum