salve ho impostato il seguente codice che mi permette di controllare se i moduli del form sono vuoti o anche se mail e captcha sono scritti correttamente
Codice PHP:
function settato($str){
if(isset($_POST['obj_cancel'])){
unset($str);
}
if(isset($str)){
return($str);
}
}
function error($str){
if(isset($_POST['obj_submit'])){
if (empty($str)){
$error="<span style=\"color:red\">Campo vuoto</span>";
}
}
return ($error);
}
if(isset($_POST['obj_submit'])){
$post = $_POST;
foreach ( $post as $key => $value) {
if(substr($key,0,4) != 'obj_'){
if(empty($value) or $value==""){
$t++;
}
}
}
}
$nome= htmlspecialchars(trim($_POST['nome']));
$mail= htmlspecialchars(trim($_POST['mail']));
$sito= htmlspecialchars(trim($_POST['obj_sito']));
$motivo= htmlspecialchars(trim($_POST['motivo']));
$commento= htmlspecialchars(trim($_POST['commento']));
if(isset($_POST['obj_submit'])){
session_start();
$codiceverifica= $_POST['codiceverifica'];
if(sha1($codiceverifica) != $_SESSION['control']){
if($codiceverifica != ""){
$error_code= "<span style=\"color:red\">Campo non corretto</span>
";
$t++;
}
}
if(isset($mail) AND $mail != ""){
if (!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$", $mail)){
$error_mail="<span style=\"color:red\">Email scritta non correttamente</span>";
$t++;
}
}
}
if(isset($_POST['obj_submit']) AND $t==0){
$header = "From: Atuttascienza<$mail>\r\n";
$subject= "Contatti Atuttascienza";
$to="xxxx@xx.it";
$messaggio="Questo messaggio ti è stato inviato dal sito!!!\n\nNOME: $nome\n\nMAIL: $mail\n\nSito web:$sito\n\nMotivo:$motivo\n\nCommento:\n$commento";
if($t ==0){
mail($to ,$subject, $messaggio, $header);
$pathtot= base64_encode($pathtot);
header("location:inviato.php?path=$pathtot");
}
}
echo $t;
echo "<a name=\"contatti\"></a>";
echo "<form name=\"form1\" action=\"contatti.php#contatti\" method=\"post\">";
if ($t > 0){
echo "<h1 class=\"error\"><u>ATTENZIONE !!! Ci sono campi vuoti o non corretti !!!</u></h1>
";
}
?>
<label for="lab_nome">Nome:</label>
<input type="text" id="lab_nome" class="textnome" name="nome" value="<?echo settato($nome);?>" size="20"> <?echo error($nome);?></p>
<label for="lab_mail">E - mail:</label>
<input type="text" id="lab_mail" name="mail" class="textmail" value="<?echo settato($mail);?>" size="30"> <?echo error($mail);?> <?echo $error_mail;?></p>
<label for="lab_sito">Sito web [b](facoltativo)[/b]:</label>
<input type="text" name="obj_sito" class="textsito" value="<?echo settato($sito);?>" size="34"></p>
<label for="lab_motivo">Motivo:</label>
<select name='motivo' class="textmotivo">
<option value="<?echo settato($motivo);?>"> <?echo ucfirst(settato($motivo));?>
<option value='no' disabled>- - - - - - - - - - - - - - - -
<option value = 'problemi tecnici' >Problemi tecnici
<option value = 'commento sul sito' >Commento sul sito
<option value = 'suggerimenti' >Suggerimenti
<option value = 'curiosità' >Curiosità
<option value = 'altro' >Altro
</select> <?echo error($motivo);?></p>
<label for="lab_commento">Commento:</label>
<textarea class="textcommenta" cols="36" rows="3" name="commento"> <?echo settato($commento);?></textarea> <?echo error($commento);?></p>
<label for="lab_captcha">Codice di sicurezza:</label>
[img]http://www.atuttascienza.altervista.org/secur/captcha.php[/img]
Inserisci il codice in figura:
<input type="text" class="textcaptcha" name="codiceverifica" size="25"> <?echo error($codiceverifica);?> <?echo $error_code;?></p>
<input type="submit" name="obj_submit" class="subcerca" value="Invia">
<input type="submit" name="obj_cancel" class="subcerca" value="Cancella"></form>
</div></div>
tramite questo pezzo di codice
Codice PHP:
if(isset($_POST['obj_submit'])){
$post = $_POST;
foreach ( $post as $key => $value) {
if(substr($key,0,4) != 'obj_'){
if(empty($value) or $value==""){
$t++;
}
}
}
}
controllo se i campi sono vuoti e mi incrementa t di 1 per ognuno di questi campi vuoti.
io ho 5 campi ma se faccio echo $t me ne conta 4 vuoti facendo la prova.
in pratica non mi conta la textarea come mai?
[/PHP]