Per wishkeeper ecco il codice (con i commenti più importanti) delle due solzioni che ho trovato...
Spero possiamo lasciare la discussione qui sul forum in questa sezione... anche a vantaggio di altri utenti... altirmenti se i moderatori reputano opportuno chiuderla (dai... siate indulgenti...) ti mando i file via mail...
ciao e buon lavoro...
Dunque, avevo bisogno di visualizzare i dati inseriti in una form generando un file...
ho trovato un metodo in php (dietro consiglio di ricman), quindi lato server, e un metodo lato client, in javascript, che posso utilizzare anche in locale, io la uso su una usb...
(occhio però che anche se funzionano, sono metodi di un principiante... c'è sicuramente roba moooolto migliore... insomma sono perfezionabili... io ci sto studiando... con la scusa imparo molto...
il secondo metodo ad esempio è pieno di roba che non serve, il codice js andrebbe pulito, ma tra tutte le ricerche che ho fatto per trovarlo non so più risalire all'esempio originale, nella cui pagina magari c'era qualche dritta su come procedere... era un webquiz, ricordo solo questo, magari fate una ricerca... boh...)
metodo lato server, in php:
Salvalo come nome_file.php
Codice PHP:
<html>
<head>
<script type="text/javascript" src="webtookit.openwindow.js"></script>
</head>
<body>
<?php
$domandaask ="chi è stato?";
$domandaask2 ="cosa è stato?";
$path = "elaborato"; //Cartella dove verrà creato il file, SENZA LO SLASH FINALE (devi crearla tu non te la genera da solo!!)
function create($name, $autore, $text, $textdue, $domandaask, $domandaask2) {
global $path, $result;
if(empty($name) || empty($autore) || empty($text) || empty($textdue)) {
$result = "CampiVuoti";
} else {
if(is_writable($path)) {
$name = addslashes(htmlentities($name, ENT_QUOTES));
$autore = nl2br(addslashes(htmlentities($autore, ENT_QUOTES)));
$text = nl2br(addslashes(htmlentities($text, ENT_QUOTES)));
$textdue = nl2br(addslashes(htmlentities($textdue, ENT_QUOTES)));
if(check_name($name)) {
$open = fopen($path."/".$name.".html", "w+");
$write = fwrite($open, $separatore = "
");
$write = fwrite($open, $separatore = "Dati di: ");
$write = fwrite($open, $autore);
$write = fwrite($open, $separatore = "<hr>");
$write = fwrite($open, $domandaask);
$write = fwrite($open, $separatore = "
");
$write = fwrite($open, $text);
$write = fwrite($open, $separatore = "
");
$write = fwrite($open, $domandaask2);
$write = fwrite($open, $separatore = "
");
$write = fwrite($open, $textdue);
$write = fwrite($open, $separatore = "<hr>
<a href=\"../\">ripeti tutto</a>");
fclose($open);
$result = "FileScritto";
} else {
$result = "NomeEsistente";
}
} else {
$result = "NotWritable";
}
}
return $result;
}
//CONTROLLA SE ESISTE UN FILE CON LO STESSO NOME
function check_name($name) {
global $path;
if(file_exists($path."/".$name.".html")) {
return false;
} else {
return true;
}
}
if(!isset($_POST['Submit'])) {
echo "<form method=\"post\" action=\"".$_SERVER['php_self']."\">\n";
echo "Nome e Cognome: <input type=\"text\" size=\"30\" value=\"nome e cognome\" name=\"autore\" />
\n";
echo "$domandaask
<textarea rows=\"10\" cols=\"50\" name=\"text\">Testo</textarea>
\n";
echo "$domandaask2
<textarea rows=\"10\" cols=\"50\" name=\"textdue\">Testo</textarea>
\n";
echo "Nome che intendi assegnare al tuo elaborato: <input type=\"text\" size=\"30\" value=\"Nome File\" name=\"name\" />
\n";
echo "Genera un report con i dati inseriti
\n";
echo "<input type=\"submit\" value=\"Genera elaborato\" name=\"Submit\" />
\n";
echo "</form>\n";
} else {
$name = $_POST['name'];
$autore = $_POST['autore'];
$text = $_POST['text'];
$textdue = $_POST['textdue'];
$result = create($name, $autore, $text, $textdue, $domandaask, $domandaask2);
if($result == "CampiVuoti") {
echo "Riempire tutti i campi
<a href=\"\">Torna indietro</a>";
} else {
if($result == "NotWritable") {
echo "Non ti è consentito creare file in questa cartella
<a href=\"\">Riprova</a>";
} else {
if($result == "NomeEsistente") {
echo "Nome del file già presente
<a href=\"\">Torna indietro</a>";
} else {
echo "File Scritto con successo
<a href=\"elaborato/$name.html\">visualizzalo</a> -
<a href=\"elaborato/$name.html\" onclick=\"return openWindow(this, {width:790,height:450,center:true,menubar:1})\">
visualizzalo in popup</a>
<a href=\"\">ripeti tutto</a>";
}
}
}
}
?>
</body>
</html>
metodo lato client, in javascript
salva come nome_file.htm (occhio ai commenti!!)
codice:
<html>
<head>
<script language=javascript src="compila_form.js"></script>
</head>
<body onLoad="document.WapForm.reset();if (waitTime>0) checkTime();">
testo testo testo</p>
<hr>
<!-- occhio che ciò che viene dopo Quiz.UserData. sarà la label del dato inserito nel campo della form:
quindi per Quiz.UserData.HOBBY otterrai una risposta del tipo HOBBY: ciò che hai inserito nel campo hobby ) -->
<form name="WapForm">
Nome: <input type="text" name="Quiz.UserData.Nome" size=20>
</p>
Cognome: <input type="text" name="Quiz.UserData.Cognome" size=20>
</p>
Altro dato: <input type="text" name="Quiz.UserData.altro dato" size=20>
</p>
Altro dato ancora: <textarea type="text" cols="30" rows="20" name="Quiz.UserData.altro dato ancora"></textarea>
</p>
<hr>
<input type="button" value="Visualizza" name="ButtonSubmit" onClick="correct(document.WapForm)">
<input type="reset" value="Pulisci" name="ButtonReset">
</form>
</body>
</html>
salva come compila_form.js
codice:
function Question (qname,
type,
qstring,
response,
comment,
corrects,
explan,
score,
ifcorrect,ifwrong,ifnull,
img)
{this.qname=qname;
this.type=type;
this.qstring=qstring;
this.response=response;
this.comment=comment;
this.corrects=corrects;
this.explan=explan;
this.score=score;
this.ifcorrect=ifcorrect;
this.ifwrong=ifwrong;
this.ifnull=ifnull;
this.img=img;
}
var zin=1,top=0, mycount=0, waitTime=0, qright=0, mycomment;
var global=new Array(3);
var recent, recent2, recdone=false, opera7, opera=CheckOpera56();
function cachewrite(s,idx){global[idx]+=s;}
function CheckOpera56()
{
var version;
if (navigator.userAgent.toLowerCase().indexOf('opera') == -1) return false;
version=parseInt(navigator.appVersion.toLowerCase());
if (version>6) {opera7=true; return false;}
if (version<5) return false;
return true;
}
questions = new Array ()
function fill(s,l){
s=s+""
for (y=1;y<=l;y++)
if (s.length>=l) break; else s="0"+s;
return s
}
function CheckQName(wapf,ii,i,multi,selection){
var len;
if (!multi) return(wapf.elements[ii].name==questions[i].qname);
len=questions[i].qname.length;
if (wapf.elements[ii].name.substring(0,len)!=questions[i].qname) return false;
if (wapf.elements[ii].name.substring(len,len+1)!="_") return false;
if (eval(wapf.elements[ii].name.substring(len+1,len+3))==(selection+1)) return true;
return false;
}
function testIE5plus(){
var pos=navigator.appVersion.lastIndexOf('MSIE ');
if (pos != -1) {
pos+=5;
if (eval(navigator.appVersion.charAt(pos))>4)
return true;}
return false;}
function correct(wapf)
{
var i, ii, t, re, tmp, selection, multi, multipage=0, type, isnull, iswrong, iscorrect, evaluation=0, total=0, udat;
errori=waitTime=0;
udat=new Array();
for (i=0, ii=0; i<wapf.elements.length; i++)
{tmp=wapf.elements[i];
if (tmp.name.substring(0,13)=="Quiz.UserData"){
t=tmp.name.substring(14,tmp.name.length);
re=new RegExp("_", "g");
t=t.replace(re," ");
udat[ii++]=t+": "+tmp.value+"
";
if (opera) tmp.value="";}}
global[0]=global[2]="";
cachewrite("<html><head><title>Dai</title><BASE target='_blank'></head><body bgcolor='#FFFFFF'><font face='Verdana, Arial'><table border=0 cellpadding=0 cellspacing=0 width='100%' bgcolor='#C0C0C0'><tr><td width='100%'><font face='Verdana, Arial' size=5 color='#FFFFFF'>Report</font></td></tr></table>",0)
now= new Date()
cachewrite("
"+fill(now.getDate(),2)+"/"+fill((now.getMonth()+1),2)+"/"+now.getYear()+""+fill(now.getHours(),2)+":"+fill(now.getMinutes(),2)+"</p>",0)
if (udat.length>0) {
cachewrite("Dati inseriti
\n",0);
for (i=0; i<udat.length; i++) cachewrite(udat[i],0);}
if (opera7 && multipage) {
for (i=0; i<questions.length; i++) {
id=document.getElementById("q"+(i+1));
id2=document.getElementById("q"+(i+1)+"a");
id.style.display=id2.style.display="block";}}
for (i=0; i<questions.length; i++) {
if (opera && multipage) {
id=document.getElementById("q"+(i+1));
id2=document.getElementById("q"+(i+1)+"a");
id.style.visibility=id2.style.visibility="visible";}
type=questions[i].type;
if (type==1) multi=1;
else multi=0;
isnull=true;
iscorrect=false;iswrong=false;
selection=0;
evaluation=0;
mycomment="";
for (ii=0; ii<wapf.elements.length; ii++) {
if (CheckQName(wapf,ii,i,multi,selection)) {
if (type>=3 && wapf.elements[ii].value!="") {
isnull=false;
if (wapf.elements[ii].value.toLowerCase()==questions[i].corrects.toLowerCase()) iscorrect=true;
else iswrong=true;
++selection;}
else if (wapf.elements[ii].checked) {
if (questions[i].score!="") evaluation+=questions[i].score[selection];
if (isnull) isnull=false;
if (questions[i].corrects[selection]=="1") iscorrect=(iswrong==false)?true:false;
else {
iswrong=true;
}
if (questions[i].comment!="" && questions[i].comment[selection]!="")
mycomment+=((iscorrect || questions[i].corrects=='')?""+questions[i].qname+"
":"")+"<small>"+questions[i].comment[selection]+"
";
} else {
if (questions[i].corrects[selection]=="1") {
iswrong=true;
}}
++selection;
}}
if (multi==false && (isnull || iswrong)) {
var okresp="", z;
if (type>=3) okresp=questions[i].corrects;
else {
for (z=0; z<questions[i].corrects.length; z++) {
if (questions[i].corrects[z]==1) {
okresp=questions[i].response[z];
break;
}}}
}
if (mycomment!="") cachewrite(mycomment,2);
if (questions[i].corrects!="") ++qright;
if (isnull) evaluation+=questions[i].ifnull;
else if (iswrong) evaluation+=questions[i].ifwrong;
else if (iscorrect) evaluation+=questions[i].ifcorrect;
total+=evaluation;}
if (qright==0) global[1]="
";
cachewrite("</small><hr noshade><center><form>",2)
printest=(((navigator.appName=="Netscape") && (navigator.appVersion.charAt(0)>="4")) || (testIE5plus() == true))? "print()": "alert('Questo pulsante funziona soltanto con Netscape Navigator 4 o Microsoft Internet Explorer 5 (o versioni successive). Per stampare con gli altri browser, seleziona il comando Stampa nel menu File.')"
cachewrite("<input type='button' value='Stampa...' onClick='"+printest+"'>",2)
cachewrite("<input type='button' value='Chiudi questa finestra' onClick='window.close()'></form></center>",2)
winr=window.open('','wapres', 'titlebar=1,location=0,directories=0,menubar=1,toolbar=0,scrollbars=1,resizable=1,status=1')
for (i=0; i<3; i++) winr.document.write(global[i]);winr.document.close()
if ((opera || opera7) && multipage) {
document.forms[0].elements[0].value='Stampa...';
document.forms[0].elements[1].value='Chiudi questa finestra';
for (i=0; i<questions.length-1; i++) {
id=document.getElementById("q"+(i+1));
id2=document.getElementById("q"+(i+1)+"a");
opera?id.style.visibility=id2.style.visibility="hidden":id.style.display=id2.style.display="none";}}
}