Title: Three page sequence of forms (checkboxes and text/number boxes) with foreach in php
Tags: php;forms;session;post;foreach
Question: I would like to pass variables through 3 pages. The 1st page asks the user which music genres they like (there will eventually be 20+ genres). The 2nd page asks the user to rank the genres they have selected and the 3rd page sorts and displays their ranking.
This first page asked the user to pick which genres they like:
<form id="genre" name="genre" method="post" action="musicsell.php"&gt;
<input type="checkbox" name="genre[]" value="Rap"/&gt;Rap<br /&gt;
<input type="checkbox" name="genre[]" value="HipHop"/&gt;HipHop<br /&gt;
<input type="checkbox" name="genre[]" value="RnB"/&gt;RnB<br /&gt;
<input type="checkbox" name="genre[]" value="Rock"/&gt;Rock<br /&gt;
<input type="checkbox" name="genre[]" value="Jazz"/&gt;Jazz<br /&gt;
<p&gt;
<input type="submit" value="Next"&gt;
<br /&gt;
</p&gt;
</form&gt;
This second asks them to rank (prioritize) the genres they have selected with 1 being the best:
<body&gt;
The genre(s) you selected are: <br&gt;
<form id="form1" name="form1" method="post" action="musicresults.php"&gt;
<?php
$name = $_POST['genre'];
if(isset($_POST['genre'])) {
foreach ($name as $genre){
<input
type="number" required="required" id="<?php echo $genre ?&gt;" name="music[<?php echo $genre ?&gt;]" /&gt;
<?php echo $genre ?&gt;<br /&gt;
<?php
<input type="submit" name="button" id="button" value="Submit" /&gt;</form&gt;
</body&gt;
The third and last page sorts and echos the results:
<?php
//Get the form results (which has been converted to an associative array)     from the $_POST super global
$musicgenres = $_POST['music'];
//Sort the values by rank and keep the key associations.
asort($musicgenres, SORT_NUMERIC );
//Loop over the array in rank order to print out the values.
foreach($musicgenres as $music =&gt; $rank)
echo "$musicgenres is your $rank choice";
echo "<br&gt;";
It all works fine until the last page where I'm getting an "array to string conversion" error. Maybe I need to put in session variables but I'm not sure.
Any help would be appreciated.

Here is the accepted answer: It's exactly what the error says. It can't convert and array to a string.
Replace
echo "$musicgenres is your $rank choice";
echo "$music is your $rank choice";
Comment for this answer: Thanks. It was a simple mistake.


{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 0, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2015:00:08/09/15', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<php><forms><session><post><foreach>', 'Title': 'Three page sequence of forms (checkboxes and text/number boxes) with foreach in php', 'ViewCount': 65, 'id': 31906440, 'user_AboutMe': None, 'user_AccountId': 4290146.0, 'user_DisplayName': 'user3506743', 'user_Reputation': 45.0, 'user_Views': 14.0, 'user_id': 3506743.0}}