【问题标题】:Three page sequence of forms (checkboxes and text/number boxes) with foreach in phpphp中带有foreach的三页表单序列(复选框和文本/数字框)
【发布时间】:2015-11-01 14:14:12
【问题描述】:

我想通过 3 个页面传递变量。第 1 页询问用户他们喜欢哪种音乐类型(最终会有 20 多种类型)。第二页要求用户对他们选择的类型进行排名,第三页排序并显示他们的排名。

第一页要求用户选择他们喜欢的类型:

 <form id="genre" name="genre" method="post" action="musicsell.php">
  <input type="checkbox" name="genre[]" value="Rap"/>Rap<br />
  <input type="checkbox" name="genre[]" value="HipHop"/>HipHop<br />
  <input type="checkbox" name="genre[]" value="RnB"/>RnB<br />
  <input type="checkbox" name="genre[]" value="Rock"/>Rock<br />
  <input type="checkbox" name="genre[]" value="Jazz"/>Jazz<br />

  <p>
    <input type="submit" value="Next">
    <br />
  </p>
</form>

这一秒要求他们对他们选择的类型进行排名(优先级),其中 1 是最好的:

<body>
The genre(s) you selected are: <br>
<form id="form1" name="form1" method="post" action="musicresults.php">
<?php
$name = $_POST['genre'];

if(isset($_POST['genre'])) {
foreach ($name as $genre){

?>

<input 
type="number" required="required" id="<?php echo $genre ?>" name="music[<?php echo $genre ?>]" />
<?php echo $genre ?><br /> 

<?php
    }
} 
?>

<input type="submit" name="button" id="button" value="Submit" /></form>
</body>

第三页和最后一页对结果进行排序和回显:

<?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 => $rank)
{
   echo "$musicgenres is your $rank choice";
   echo "<br>";
}
?>

在我收到“数组到字符串转换”错误的最后一页之前一切正常。也许我需要输入会话变量,但我不确定。

任何帮助将不胜感激。

【问题讨论】:

    标签: php forms session post foreach


    【解决方案1】:

    这正是错误所说的。它不能将和数组转换为字符串。

    替换

    echo "$musicgenres is your $rank choice"; 
    

    echo "$music is your $rank choice";
    

    【讨论】:

    • 谢谢。这是一个简单的错误。
    猜你喜欢
    • 1970-01-01
    • 2011-05-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多