User Tools

Site Tools


appunti5s:elaborare_dati_in_php

Esercizi

Elaborazione dei dati ricevuti da un form con soluzione

primo esercizio

Costruire un sito web di due pagine di nome “primo.htm” e “primo.php” in cui l'utente possa inserire uno username e una password, inviarle e visualizzarle nell'altra pagina, ma che visualizzi i dati inviati.

secondo esercizio

Costruire un sito web di due pagine di nome “secondo.htm” e “secondo.php” in cui l'utente possa inserire uno username e una password, inviarle e visualizzarle nell'altra pagina, in modo che venga effettuato un controllo per evitare che le due cose coincidano: la pagina che riceve i dati visualizza un messaggio di errore nel caso in cui la password è identica allo username.

terzo esercizio

(Prerequisito conoscere foreach) Costruire un sito web di due pagine di nome terzo.htm e terzo.php in cui l'utente possa inserire, codice fiscale, nome, cognome e sesso. Il sesso deve essere un radio button (Femmina/Maschio) che invia i valori “F” oppure “M”. La pagina che riceve visualizza tutti i dati ricevuti usando foreach().

quarto esercizio

Continuare il precedente esercizio, ma se nel sesso viene inserito “F” viene visualizzato “Femmina”, se “M” viene visualizzato “Maschio”

quinto esercizio

Continuare il precedente esercizio, ma se NON e' selezionato il sesso visualizza “non hai selezionato sesso”. Suggerimento: usare la funzione empty().

sesto esercizio

il form è come sopra, ma nella pagina che riceve i dati si deve controllare l'inserimento di ogni campo prima di visualizzarne il contenuto. Quando il campo è lasciato vuoto si deve visualizzare il nome del campo, quando il campo è riempito si deve visualizzare il suo contenuto: usare foreach() ed empty().

Soluzione

Soluzione primo

primo.html
<DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
	</head>
	<body>
		<form method="post" action="primo.php">
			<p>inserire username
				<input name="user" type="text" >
			</p>
			<p> inserire password
				<input name="pass" type="password" >
			</p>
			<p>
				<input type="submit" value="invia">
			</p>
		</form>
	</body>
</html>
primo.php
<DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
	</head>
	<body>
		<h1> Sono stati ricevuti i seguenti dati:</h1>
		<?php
 
			echo "<p> questo e' lo username:";
			echo $_POST['user']."</p>";
		?>
 
	</body>
</html>

Soluzione secondo

secondo.html
<DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
	</head>
	<body>
		<form method="post" action="secondo.php">
			<p>inserire username
				<input name="user" type="text" >
			</p>
			<p> inserire password
				<input name="pass" type="password" >
			</p>
			<p>
				<input type="submit" value="invia">
			</p>
		</form>
	</body>
</html>
secondo.php
<DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
	</head>
	<body>
		<?php
			if($_POST['user']==$_POST['pass'])
			{
				echo "<p> meglio evitare che la password sia uguale allo usename </p>";
			}
			else
			{
				echo "<p> questo e' lo username da te scelto: ";
				echo $_POST['user']."</p>";
			}
 
		?>
	</body>
</html>

Soluzione terzo

terzo.html
<DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
	</head>
	<body>
		<form method="post" action="terzo.php">
			<p>inserire codice fiscale
				<input name="cf" type="text" >
			</p>
			<p> inserire nome
				<input name="nome" type="text" >
			</p>
			<p> inserire cognome
				<input name="cognome" type="text" >
			</p>
			<p> inserire sesso
				F<input name="sesso" type="radio" value="F" >
				M<input name="sesso" type="radio" value="M" >
			</p>			
			<p>
				<input type="submit" value="invia">
			</p>
		</form>
	</body>
</html>
terzo.php
<DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
	</head>
	<body>
		<?php
			foreach($_POST as $c => $v)
			{
				echo "<p>".$v."</p>";
			}
 
		?>
	</body>
</html>

Soluzione quarto

quarto.html
<DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
	</head>
	<body>
		<form method="post" action="quarto.php">
			<p>inserire codice fiscale
				<input name="cf" type="text" >
			</p>
			<p> inserire nome
				<input name="nome" type="text" >
			</p>
			<p> inserire cognome
				<input name="cognome" type="text" >
			</p>
			<p> inserire sesso
				F<input name="sesso" type="radio" value="F" >
				M<input name="sesso" type="radio" value="M" >
			</p>			
			<p>
				<input type="submit" value="invia">
			</p>
		</form>
	</body>
</html>
quarto.php
<DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
	</head>
	<body>
		<?php
			foreach($_POST as $c => $v)
			{
				echo "<p>".$v."</p>";
			}
 
			if($_POST['sesso']=="F")
			{
				echo "<p> femmina </p>";
			}
			else
			{
				echo "<p> maschio </p>";
			}
 
		?>
	</body>
</html>

Soluzione quinto

quinto.html
<DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
	</head>
	<body>
		<form method="post" action="quinto.php">
			<p>inserire codice fiscale
				<input name="cf" type="text" >
			</p>
			<p> inserire nome
				<input name="nome" type="text" >
			</p>
			<p> inserire cognome
				<input name="cognome" type="text" >
			</p>
			<p> inserire sesso
				F<input name="sesso" type="radio" value="F" >
				M<input name="sesso" type="radio" value="M" >
			</p>			
			<p>
				<input type="submit" value="invia">
			</p>
		</form>
	</body>
</html>
quinto.php
<DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
	</head>
	<body>
		<?php
 
			if(empty($_POST['sesso']))
			{
				echo "<p>non hai selezionato sesso...</p>";
 
			}
			else
			{
				if($_POST['sesso']=="F")
				{
					echo "<p> femmina </p>";
				}
				else
				{
					echo "<p> maschio </p>";
				}
 
			}
 
		?>
	</body>
</html>

Soluzione sesto

sesto.html
<DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
	</head>
	<body>
		<form method="post" action="sesso.php">
			<p>inserire codice fiscale
				<input name="cf" type="text" >
			</p>
			<p> inserire nome
				<input name="nome" type="text" >
			</p>
			<p> inserire cognome
				<input name="cognome" type="text" >
			</p>
			<p> inserire sesso
				F<input name="sesso" type="radio" value="F" >
				M<input name="sesso" type="radio" value="M" >
			</p>			
			<p>
				<input type="submit" value="invia">
			</p>
		</form>
	</body>
</html>
sesto.php
<DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
	</head>
	<body>
		<?php
			foreach($_POST as $c => $v)
			{
 
				if(empty($v))
				{
					echo "<p>non hai selezionato ".$c."</p>";
				}
				else
				{
					echo "<p>".$v."</p>";
				}
			}
 
		?>
	</body>
</html>
appunti5s/elaborare_dati_in_php.txt · Last modified: 2020/06/08 22:20 by 127.0.0.1