io ho fatto una cosa in ajax.
Risposta in php:
file: risposta-ajax.php
Codice PHP:
<?php
$profili[0][0]="profilo a. Primo questionario";
$profili[0][1]="profilo b. Primo questionario";
$profili[0][2]="profilo c. Primo questionario";
$profili[1][0]="profilo a. Secondo questionario";
$profili[1][1]="profilo b. Secondo questionario";
$profili[1][2]="profilo c. Secondo questionario";
$q=$_GET['q'];
$r[0]=$_GET['uno'];
$r[1]=$_GET['due'];
$r[2]=$_GET['tre'];
$r[3]=$_GET['quattro'];
$r[4]=$_GET['cinque'];
$r[5]=$_GET['sei'];
$a=0;
$b=0;
$c=0;
foreach($r as $risposta){
switch($risposta){
case 'a':
$a++;
break;
case 'b':
$b++;
break;
case 'c':
$c++;
break;
default:
break;
}
}
function doublemax($mylist){
$maxvalue=max($mylist);
while(list($key,$value)=each($mylist)){
if($value==$maxvalue) return $key;
}
}
echo $profili[$q][doublemax(array($a, $b, $c))];
?>
pagina in html con il sondaggio:
file: va-bene-qualsiasi-nome.html
Codice:
<html>
<head>
<script type="text/javascript" src="script1.js"></script>
</head>
<body>
<form name="form" onSubmit="return false;">
<fieldset>
<p>primo<br />
<input name="uno" type="radio" value="a">a
<br />
<input name="uno" type="radio" value="b">b
<br />
<input name="uno" type="radio" value="c">c
</p><p>
secondo<br />
<input name="due" type="radio" value="a">a
<br />
<input name="due" type="radio" value="b">b
<br />
<input name="due" type="radio" value="c">c
</p><p>
terzo <br />
<input name="tre" type="radio" value="a">a
<br />
<input name="tre" type="radio" value="b">b
<br />
<input name="tre" type="radio" value="c">c
</p><p>
quarto <br />
<input name="quattro" type="radio" value="a">a
<br />
<input name="quattro" type="radio" value="b">b
<br />
<input name="quattro" type="radio" value="c">c
</p><p>
quinto <br />
<input name="cinque" type="radio" value="a">a
<br />
<input name="cinque" type="radio" value="b">b
<br />
<input name="cinque" type="radio" value="c">c
</p><p>
sesto <br />
<input name="sei" type="radio" value="a">a
<br />
<input name="sei" type="radio" value="b">b
<br />
<input name="sei" type="radio" value="c">c
</p>
<input type="submit" name="Submit" onClick="inviaForm()" />
</fieldset>
</form>
<div id="messaggio"></div>
</body>
</html>
pagina in javascript:
file: script1.js
Codice:
var myRequest = null;
//questa variabile può essere 0 oppure 1. dipende dal questionario che si vuole fare e da come sono settati i questionari nel file risposta.php
var quest = 0;
function CreateXmlHttpReq(handler) {
var xmlhttp = null;
try {
xmlhttp = new XMLHttpRequest();
} catch(e) {
try {
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch(e) {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
}
xmlhttp.onreadystatechange = handler;
return xmlhttp;
}
function myHandler() {
if (myRequest.readyState == 1) {
//cambia("loading");
}
if (myRequest.readyState == 4 ) {
//alert(myRequest.responseText);
cambia(myRequest.responseText);
}
}
function cambia(nome){
var msg = document.getElementById("messaggio");
if(nome=="loading"){
msg.innerHTML = " Attendere...";
}else{
msg.innerHTML=nome;
}
}
function inviaForm() {
myRequest = CreateXmlHttpReq(myHandler);
var rad_val=null;
for (var i=0; i < document.form.uno.length; i++){
if (document.form.uno[i].checked){
rad_val = "uno="+document.form.uno[i].value;
}
}
for (var i=0; i < document.form.due.length; i++){
if (document.form.due[i].checked){
rad_val = rad_val+"&due="+document.form.due[i].value;
}
}
for (var i=0; i < document.form.tre.length; i++){
if (document.form.tre[i].checked){
rad_val = rad_val+"&tre="+document.form.tre[i].value;
}
}
for (var i=0; i < document.form.quattro.length; i++){
if (document.form.quattro[i].checked){
rad_val = rad_val+"&quattro="+document.form.quattro[i].value;
}
}
for (var i=0; i < document.form.cinque.length; i++){
if (document.form.cinque[i].checked){
rad_val = rad_val+"&cinque="+document.form.cinque[i].value;
}
}
for (var i=0; i < document.form.sei.length; i++){
if (document.form.sei[i].checked){
rad_val = rad_val+"&sei="+document.form.sei[i].value;
}
}
// alert(rad_val);
myRequest.open("GET","risposta-ajax.php?q="+quest+"&"+rad_val);
myRequest.send(null);
return false;
}
a chi potesse servire...