Avendo seguito un corso e avendo per adesso praticato solo il metodo procedurale di php mi sto avvicinando alla programmazione ad oggetti ed ho buttato giù qualcosa per vedere se il senso logico e la sintassi riuscivo a seguirle premettendo che non mi da nessun errore ma non fa quello richiesto cioè verificare se l'email è valida se i campi sono vuoti e se il form è stato rinviato tramite aggiorna pagina.
Codice PHP:
<?php
class checkForm{
public $error = array();
function checkEmail($email_check){
$re = "^([a-zA-Z0-9])+([\.a-zA-Z0-9_-])*@([a-zA-Z0-9_-])";
$re .= "+(\.[a-zA-Z0-9_-]+)*\.([a-zA-Z]{2,6})$";
if (ereg($re, $email_check))
{
return TRUE;
}
else
{
return FALSE;
echo $this->error("Email Non Valida");
}
}
function emailNull($email_null){
if($email_null == ""){
return FALSE;
$this->error("Campo Vuoto");
}else{
return TRUE;
}
}
function formToken($form_token){
$time_send = $_POST['form_token'];
if($this->time_send == $form_token){
return FALSE;
$this->error("Form Inviato");
}else{
return TRUE;
}
}
}
$email = trim($_POST['email']);
$nome = trim($_POST['nome']);
if (isset($_POST['submit'])){
$check = new checkForm;
$check->checkEmail($email);
$check->emailNull($email);
$check->emailNull($nome);
$check->formToken($time_send);
if ($check == TRUE){
echo"email inviata";
}else{
echo "errore";
}
}else{
?>
<html>
<head>
<title>Check Form</title>
<style type="text/css">
label{display:block;}
</style>
</head>
<body>
<form method="post" action="">
<label for="name">Nome</label>
<input type="text" name="nome" />
<label for="email">Email</label>
<input type="text" name="email" />
<input type="hidden" name="form_token" value="<?php echo ($time_send =mktime()); ?>" />
<input type="submit" name="submit" value="Invia" />
</form>
<?php
}
?>
</body>
</html>
Mi date delle dritte su dove sbaglio e se è corretto programmare così ad oggetti?
Grazie