Prova così :

Codice PHP:
<html>
<head>
<SCRIPT LANGUAGE="JavaScript">
function ver()
{
for (i=0; i<document.forms[0].elements.length; ++i)
if(document.forms[0].elements[i].value == "")
{
alert("Inserire testo nei campi!");
document.forms[0].elements[i].focus();
return false;
}
return true; 
}
</SCRIPT>
</head>
<body>
<?php
if($_POST['Submit']) {
    
//Pezzo di codice per la creazione di un nuovo test
    
if (empty($_POST['righe'])) {
        
$righe=0;
    }else {
        
$righe=$_POST['righe'];
    } 
    if (empty(
$_POST['fatto'])) {
        
$fatto=false;
    }else {
        
$fatto=$_POST['fatto'];
    }
    if (empty(
$_POST['testa'])) {
        
$testa=0;
    }else {
        
$testa=$_POST['testa'];
    }
    if (empty(
$_POST['nome'])) {
        
$nome='vuoto';
    }else {
        
$nome=$_POST['nome'];
    }
    if (empty(
$_POST['codice'])) {
        
$codice='';
    }else {
        
$codice=$_POST['codice'];
    }
    if (empty(
$_POST['inizio'])) {
        
$inizio=0;
    }else {
        
$inizio=$_POST['inizio'];
        
$f=fopen('sondaggi/'.$codice.'.txt','a+');
    
////////////////////// 
    //Vado a prendere la domanda
    
$dom=$_POST['domanda'];
    
// echo'domanda:'.$dom.'
';
    // echo'
inizio:'.$inizio.'
'; 
    //Vado a scrivere la domanda nel file 
    //Vado a comporre la stringa da salvare
    $stringa='
#'.$inizio.'#dom#'.$dom.'#';
    
fwrite($f,$stringa."\r\n");
    
//Vado a scrivere la prima risposta quella esatta sul file
    //Vado a comporre la stringa da salvare
    
$risp1=$_POST['risp1'];
    
// echo'risp1:'.$risp1.'
';
    $stringa='
#'.$inizio.'#ris#'.$risp1.'#si#';
    
fwrite($f,$stringa."\r\n");
    
//Vado a scrivere la seconda risposta sul file
    //Vado a comporre la stringa da salvare
    
$risp2=$_POST['risp2'];
    
// echo'risp2:'.$risp2.'
';
    $stringa='
#'.$inizio.'#ris#'.$risp2.'#no#';
    
fwrite($f,$stringa."\r\n");
    
//Vado a scrivere la terza risposta sul file
    //Vado a comporre la stringa da salvare
    
$risp3=$_POST['risp3'];
    
// echo'risp3:'.$risp3.'
';
    $stringa='
#'.$inizio.'#ris#'.$risp3.'#no#';
    
fwrite($f,$stringa."\r\n");
    
//Vado a scrivere la quarta risposta sul file
    //Vado a comporre la stringa da salvare
    
$risp4=$_POST['risp4'];
    
// echo'risp4:'.$risp4.'
';
    $stringa='
#'.$inizio.'#ris#'.$risp4.'#no#';
    
fwrite($f,$stringa."\r\n");
    
    
////////////////////////////
    
fclose($f);
    }
    
    if ((
$righe==0) or ($nome=='vuoto')or ($righe>10)){
    
//Vado la prima domanda che serve per creare tutto il test
    
print'<form action="nuovo_test.php" method="post">';
    print
'Tuo nome?
'
;
    print
'<input type="text" name="nome" >
'
;
    print
'Quante domande vuoi creare?(max 10)
'
;
    print
'<input type="text" name="righe" >
'

    print
'<input type="submit" name="Submit" value="Genera">';
    print
'</form>' ;
    
    
// echo'pd';
    
}else {
    
    
    
//Vado a generare un codice casuale.. per andare a salvare il file
    //////////////////////////
    
while (!$fatto) {
    
    
// Lunghezza del codice da generare
    
$lunghezza_codice 10;
    for ( 
$k=1;$k<=$lunghezza_codice;$k++ )
    {
    
// $k multiplo di 3
    
if ( $k )
    {
    
// Scegliamo se maiuscola o minuscola
    
if ( rand0,200 ) <= 100 )
    {
    
// Maiuscola
    // Nella tabella ASCII da 65 a 90 ci sono le lettere dell'alfabeto Maiuscole
    
    
$codice .= chrrand 65,90 ) );
    }
    else
    {
    
// Minuscola
    // Nella tabella ASCII da 97 a 122 ci sono le lettere dell'alfabeto Minuscole
    
    
$codice .= chrrand 97,122 ) );
    }
    }
    
// $k non è multiplo di 3
    
else
    {
    
// Inseriamo un numero
    
$codice .= rand0,);
    }
    }
    
//Vado a vedere se il codice del file è presente o no
    
$handle=opendir('sondaggi/');
    while (
$file readdir($handle))
    {
    
// Vado ad eleminare . e .. dalla visualizzazione
    
if ($file != "." && $file != "..") {
    
//Vado a prendere l'estensione del file
    
$estensione strtolower(substr($filestrrpos($file"."), strlen($file)-strrpos($file".")));
    
//echo $estensione.'
';
    //Vado a stampare solo i file .txt
    if ($estensione=='
.txt') {
    //Vado a prendere solamente il nome del file escludendo il .txt
    $file = substr($file, 0,strrpos($file, "."));
    //echo $file.'
';
    
    if ($file!=$codice) {
    $fatto=true;
    //echo'
elemento diverso' ;
    } 
    } 
    }else {
    $fatto=true;
    }
    } 
    $handle = closedir($handle);
    
    }
    
    /////
    if ($inizio<$righe) {
    if ($testa===0) {
    //Parte che vado ad eseguire solamente una volta
    //Vado a scrivere il file
    $f=fopen('
sondaggi/'.$codice.'.txt','a+');
    //Vado a comporre la stringa da salvare
    $stringa='
#num#'.$righe.'#'.$nome.'#';
    
fwrite($f,$stringa."\r\n");
    
fclose($f);
    
// echo'ciao';
    
}
}
//Se tutti i campi non sono pieni non si può andare avanti
print'<form action="nuovo_test.php" method="post"onSubmit="return ver();" name="Submit">';
//Vado a creare la domanda più le risposte
$inizio++;
print
'<label><font color="red">Domanda n°'.$inizio.'</font></label><input type="text" name="domanda">

'
;
print
'<label>Risposta Esatta    </label><input type="text" name="risp1">

'
;
print
'<label>Altra Risposta    </label><input type="text" name="risp2">

'
;
print
'<label>Altra Risposta    </label><input type="text" name="risp3">

'
;
print
'<label>Altra Risposta    </label><input type="text" name="risp4">

'
;

print
'<input type="hidden" name="inizio" value="'.$inizio.'">';
print
'<input type="hidden" name="nome" value="'.$nome.'">';
print
'<input type="hidden" name="fatto" value="'.$fatto.'">'
print
'<input type="hidden" name="righe" value="'.$righe.'">'
print
'<input type="hidden" name="codice" value="'.$codice.'">'
print
'<input type="hidden" name="testa" value="1">'
print
'<input type="submit" name="Submit" value="avanti">';
print
'</form>' ;
//print 'Nome:'.$nome.'
';
// print '
Codice:'.$codice.'
';
// print '
Domande:'.$righe.'
';
// print '
Inizio:'.$inizio.'
';
// print '
Testa:'.$testa.'
';
}else {


//Vado a dire operazione conclusa
//echo'
Pd';
echo'
<label>Ecco il link del tuo testpuoi fare copia incolla da qui:</label>
';
echo'
<input type="text" value=http://www.dav1.altervista.org/test/visualizza.php?nome='.$codice.' name="codice"size=100>
';
print'
[url="index.html"]Torna alla Home[/url]';
}
}
?>
</body> 
</html>