Forum Moderators: coopster

Message Too Old, No Replies

getting array values from a form

array, quizz, form,php

         

hanyaz

3:29 pm on Apr 16, 2008 (gmt 0)

10+ Year Member



hello,
i am trying to make a quizz, where my visitors will have to fill a form (text fields) and then when they click on post the sentences appear with the correction.
i have made a piece of code for that but i am a newbie, any help would be appreciated.

<?php
function error($myproblem) {

echo "<b> $myproblem </b>";

}
//question for my quizz
$monquizz=array (
array ("1","i am ","at","home"),
array ("2","i go","to","university"),
array ("3","i drink","my","beer"),
);
//end

$reponse = array(); // vide, destiné à recevoir les saisies

$saisie_reponse[] = ""; // saisies postées via formulaire

if (isset($_POST['action']))
{

for ($i=0; $i<count($monquizz); $i++) {
foreach ($_POST['saisie_reponse'] as $reponse){

$correct=$monquizz[$i][2];//la bonne reponse

echo "$correct $reponse<br>";

}//end of for

}

}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Document sans titre</title>
</head>

<body>

<form id="quizz" name="quizz" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<?php

foreach($saisie_reponse as $reponse){
for ($i=0; $i<count($monquizz); $i++) {

$question="question".$monquizz[$i][0];
echo $monquizz[$i][1];

?>
<input type="text" name="saisie_reponse[]" value="<?php echo $reponse; ?>" size="30" maxlength="35">
<?php

echo $monquizz[$i][3];

echo"<br>";
echo"<br>";
}
}
?> <label>
<input name="action" type="hidden" value="valider" />

<input type="submit" name="$ajout_reponse" value="Envoyer" />
</label>
</form>
<p>
</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
</body>
</html>

Thanks in advance
Hanlin

cameraman

6:40 pm on Apr 16, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Where you're building your form, you have this:
foreach($saisie_reponse as $reponse){
for ($i=0; $i<count($monquizz); $i++) {

But $saisie_reponse is an empty array before the form has been submitted, so the loop won't display anything. Remove the foreach and its corresponding }.

You'll also want to add some markup between the questions or they'll all get stuck together on one line. Something like this perhaps:
echo "<p>" . $question . $monquizz[$i][1] . "</p>";