Jump to content

[SOLVED] From Post to Array?


jandrews3

Recommended Posts

I am receiving from a form of 1-50, variables answer1 through answer50 for example. There is a way to initiate some kind of temporary variable like below so that they can essentially be treated as an array, but I do not know how. If someone could tell me what would get the code below to work, then I know I could incorporate the concept into my page. Thank you.

$count = 0;
$max = 49;
while ($count <= $max){
	print "You typed: ".$answer.$count."<br>";
	print "You were asked: ".$new.$count."<br>";
	print "The answer was: ".$ans.$count."<br>";
	$count++;
}

 

 

Link to comment
https://forums.phpfreaks.com/topic/170511-solved-from-post-to-array/
Share on other sites

You should use an array in your form - http://us.php.net/manual/en/faq.html.php#faq.html.arrays

 

Use array index names that are the entry number -

<input name="answer[1]" />
<input name="answer[2]" />
<input name="answer[3]" />
...

 

$arr = range(1,50);

forEach($arr as $v)
{
   echo "\t" . '<input type="text" name="answer[' . $v . ']" size="30" ><br >' . "\n\r";
}
// when data is posted

forEach($_POST['answer'] as $k => $v)
{
   echo "\t\t" . 'Q' . $k . '. You answered: ' . stripslashes(HTMLentities($v, ENT_QUOTES)) . '<br ><br >' . "\n\r";
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.