PDA

View Full Version : [PHP] Un problema con i forms


Goasoul
11-02-2008, 21:32
Ciao a tutti io ho creato la seguente pagina php:

CODE[
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Caselline</title>
</head>
<body>
<form action="Caselline.php" method="GET">
<h1>Impariamo le caselline</h1>

<select name="eNuovoNum" size="1">
<option>1</option>
<option>2</option>
<option>3</option>
<option selected>4</option>
<option>5</option>
<option>6</option>
<option>7</option>
<option>8</option>
<option>9</option>
<option>10</option>
</select>
<input type="submit" name="bCarica" value="Carica nuovo valore"><br /><br/>
<h2>Completa le caselline con i valori mancanti</h2>

<table border="0">

<tr><td width="40"><?php print $_GET["eNuovoNum"];?></td><td width="40">x</td><td width="40">1</td><td width="40"> = </td><td width="90"><input name="eVal1" size="2" value="0"/></td> <td><?php if($_GET["eNuovoNum"]*1 == $_GET["eVal1"])print("Corretto!");else print("Errato"); ?></td></tr>
<tr><td width="40"><?php print $_GET["eNuovoNum"];?></td><td width="40">x</td><td width="40">2</td><td width="40"> = </td><td width="90"><input name="eVal2" size="2" value="0"/></td> <td><?php if($_GET["eNuovoNum"]*2 == $_GET["eVal2"])print("Corretto!");else print("Errato"); ?></td></tr>
<tr><td width="40"><?php print $_GET["eNuovoNum"];?></td><td width="40">x</td><td width="40">3</td><td width="40"> = </td><td width="90"><input name="eVal3" size="2" value="0"/></td> <td><?php if($_GET["eNuovoNum"]*3 == $_GET["eVal3"])print("Corretto!");else print("Errato"); ?></td></tr>
<tr><td width="40"><?php print$_GET["eNuovoNum"];?></td><td width="40">x</td><td width="40">4</td><td width="40"> = </td><td width="90"><input name="eVal4" size="2" value="0"/></td> <td><?php if($_GET["eNuovoNum"]*4 == $_GET["eVal4"])print("Corretto!");else print("Errato"); ?></td></tr>
<tr><td width="40"><?php print $_GET["eNuovoNum"];?></td><td width="40">x</td><td width="40">5</td><td width="40"> = </td><td width="90"><input name="eVal5" size="2" value="0"/></td> <td><?php if($_GET["eNuovoNum"]*5 == $_GET["eVal5"])print("Corretto!");else print("Errato"); ?></td></tr>
<tr><td width="40"><?php print $_GET["eNuovoNum"];?></td><td width="40">x</td><td width="40">6</td><td width="40"> = </td><td width="90"><input name="eVal6" size="2" value="0"/></td> <td><?php if($_GET["eNuovoNum"]*6 == $_GET["eVal6"]) print("Corretto!");else print("Errato"); ?></td></tr>
<tr><td width="40"><?php print $_GET["eNuovoNum"];?></td><td width="40">x</td><td width="40">7</td><td width="40"> = </td><td width="90"><input name="eVal7" size="2" value="0"/></td> <td><?php if($_GET["eNuovoNum"]*7 == $_GET["eVal7"])print("Corretto!");else print("Errato"); ?></td></tr>
<tr><td width="40"><?php print $_GET["eNuovoNum"];?></td><td width="40">x</td><td width="40">8</td><td width="40"> = </td><td width="90"><input name="eVal8" size="2" value="0"/></td> <td><?php if($_GET["eNuovoNum"]*8 == $_GET["eVal8"])print("Corretto!");else print("Errato"); ?></td></tr>
<tr><td width="40"><?php print $_GET["eNuovoNum"];?></td><td width="40">x</td><td width="40">9</td><td width="40"> = </td><td width="90"><input name="eVal9" size="2" value="0"/></td> <td><?php if($_GET["eNuovoNum"]*9 == $_GET["eVal9"])print("Corretto!");else print("Errato"); ?></td></tr>
<tr><td width="40"><?php print $_GET["eNuovoNum"];?></td><td width="40">x</td><td width="40">10</td><td width="40"> = </td><td width="90"><input name="eVal10" size="2" value="0"/>
</td><td><?php if($_GET["eNuovoNum"]*10 == $_GET["eVal10"])print("Corretto!");else print("Errato"); ?></td></tr>
</table>
<input type="submit" name="bVerifica" value="Verifica" />
<input type="hidden" name="num" value="<?=$_GET["eNuovoNum"];?>" />


</form>

</body>
</html>
]CODE

ed ho i seguenti problemi:

1.Carico la pagina la colonna dei numeri che ho dovrebbero incolonnarsi non appaiono fino a quando non clicco sul pulsante "Carica nuovo numero" e poi funziona.

2. Quando non immetto nulla nella text box per i risultati mi appare la scritta "Errato" mentre il mio intento sarebbe di farlo apparire solo una volta che io clicco il bottone Verifca.

3. Vorrei che una volta che ho premuto il bottone verifica il risultato immesso non sparisca ma rimanga.

E' un po' che cerco anche in internet ma purtroppo non ho trovato nulla... Se qualcuno di voi esperti in php mi dareste una mano vi sarei infinitamente grato...

Ciao a tutti

amedeoviscido
14-02-2008, 12:00
Ecco il tuo programma corretto:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Caselline</title>
</head>
<body>
<form action="Caselline.php" method="GET">
<h1>Impariamo le caselline</h1>

<?php
$valore = $_GET["eNuovoNum"];
if ($valore=="") $valore=4;
?>

<select name="eNuovoNum" size="1">
<option <?php if ($valore==1) print "selected"; ?>>1</option>
<option <?php if ($valore==2) print "selected"; ?>>2</option>
<option <?php if ($valore==3) print "selected"; ?>>3</option>
<option <?php if ($valore==4) print "selected"; ?>>4</option>
<option <?php if ($valore==5) print "selected"; ?>>5</option>
<option <?php if ($valore==6) print "selected"; ?>>6</option>
<option <?php if ($valore==7) print "selected"; ?>>7</option>
<option <?php if ($valore==8) print "selected"; ?>>8</option>
<option <?php if ($valore==9) print "selected"; ?>>9</option>
<option <?php if ($valore==10) print "selected"; ?>>10</option>
</select>
<input type="submit" name="bCarica" value="Carica nuovo valore"><br /><br/>
<h2>Completa le caselline con i valori mancanti</h2>

<table border="0">

<tr><td width="40"><?php print $valore;?></td><td width="40">x</td><td width="40">1</td><td width="40"> = </td><td width="90"><input name="eVal1" size="2" value="<?php print $_GET['eVal1']; ?>"/></td> <td><?php if($valore*1 == $_GET["eVal1"])print("Corretto!");else print("Errato"); ?></td></tr>
<tr><td width="40"><?php print $valore;?></td><td width="40">x</td><td width="40">2</td><td width="40"> = </td><td width="90"><input name="eVal2" size="2" value="<?php print $_GET['eVal2']; ?>"/></td> <td><?php if($valore*2 == $_GET["eVal2"])print("Corretto!");else print("Errato"); ?></td></tr>
<tr><td width="40"><?php print $valore;?></td><td width="40">x</td><td width="40">3</td><td width="40"> = </td><td width="90"><input name="eVal3" size="2" value="<?php print $_GET['eVal3']; ?>"/></td> <td><?php if($valore*3 == $_GET["eVal3"])print("Corretto!");else print("Errato"); ?></td></tr>
<tr><td width="40"><?php print $valore;?></td><td width="40">x</td><td width="40">4</td><td width="40"> = </td><td width="90"><input name="eVal4" size="2" value="<?php print $_GET['eVal4']; ?>"/></td> <td><?php if($valore*4 == $_GET["eVal4"])print("Corretto!");else print("Errato"); ?></td></tr>
<tr><td width="40"><?php print $valore;?></td><td width="40">x</td><td width="40">5</td><td width="40"> = </td><td width="90"><input name="eVal5" size="2" value="<?php print $_GET['eVal5']; ?>"/></td> <td><?php if($valore*5 == $_GET["eVal5"])print("Corretto!");else print("Errato"); ?></td></tr>
<tr><td width="40"><?php print $valore;?></td><td width="40">x</td><td width="40">6</td><td width="40"> = </td><td width="90"><input name="eVal6" size="2" value="<?php print $_GET['eVal6']; ?>"/></td> <td><?php if($valore*6 == $_GET["eVal6"]) print("Corretto!");else print("Errato"); ?></td></tr>
<tr><td width="40"><?php print $valore;?></td><td width="40">x</td><td width="40">7</td><td width="40"> = </td><td width="90"><input name="eVal7" size="2" value="<?php print $_GET['eVal7']; ?>"/></td> <td><?php if($valore*7 == $_GET["eVal7"])print("Corretto!");else print("Errato"); ?></td></tr>
<tr><td width="40"><?php print $valore;?></td><td width="40">x</td><td width="40">8</td><td width="40"> = </td><td width="90"><input name="eVal8" size="2" value="<?php print $_GET['eVal8']; ?>"/></td> <td><?php if($valore*8 == $_GET["eVal8"])print("Corretto!");else print("Errato"); ?></td></tr>
<tr><td width="40"><?php print $valore;?></td><td width="40">x</td><td width="40">9</td><td width="40"> = </td><td width="90"><input name="eVal9" size="2" value="<?php print $_GET['eVal9']; ?>"/></td> <td><?php if($valore*9 == $_GET["eVal9"])print("Corretto!");else print("Errato"); ?></td></tr>
<tr><td width="40"><?php print $valore;?></td><td width="40">x</td><td width="40">10</td><td width="40"> = </td><td width="90"><input name="eVal10" size="2" value="<?php print $_GET['eVal10']; ?>"/></td><td><?php if($valore*10 == $_GET["eVal10"])print("Corretto!");else print("Errato"); ?></td></tr>
</table>
<input type="submit" name="bVerifica" value="Verifica" />
<input type="hidden" name="num" value="<?=$_GET["eNuovoNum"];?>" />


</form>

</body>
</html>

Ti consiglio di STUDIARTI per bene la soluzione che ti ho proposto, in particolare l'introduzione della variabile "$valore" e del fatto che tu stampavi a schermo sempre "0" e non il valore inviato dal form tramite il pulsantino controlla.
La scritta "Errato" compare giustamente sempre: prova ad elaborare una soluzione. Suggerimento: usa una variabile ausiliaria booleana che viene settata a true se il valore inizialmente selezionato era vuoto (che avviene soltanto durante il 1° caricamento della pagina)

Goasoul
14-02-2008, 12:29
Grazie mille per il tuo aiuto... Ora proverņ ad implementare ed aggiungere il codice mancante... Grazie ancora di tutto cuore!