然后显示Form中的PHP数组

时间:2015-02-27 09:03:57

标签: php arrays html-form

我正在尝试构建一些允许用户将名称输入到表单文本字段中并使用逗号分隔的内容以及何时将其提交到提交然后将其放入数组并打开将这些人归为组的页面of x(也由用户选择)。我有使用硬编码静态数组构建的代码。但我对PHP Post很新,所以不确定如何做到这一点。有人可以帮忙吗?这是我到目前为止所做的。

<div data-role="page" id="pg_teambuilder">
    <div data-role="header" class="center">
        <span>Team Builder</span>
    </div>
    <div data-role="tabs" id="tabs">
        <div data-role="navbar">
            <ul>
                <li><a href="#input" data-ajax="false">Input Values</a></li>
                <li><a href="#random" data-ajax="false">Randomizer</a></li>
            </ul>
        </div>
        <div id="input" class="ui-body-d ui-content">
            <form>
                <ul data-role="listview" data-inset="true">
                    <li class="ui-field-contain">
                        <label for="csplayers">Players (Separate by commas):</label>
                        <textarea cols="40" rows="8" name="csplayers" id="csplayers"></textarea>
                    </li>
                    <li class="ui-body ui-body-b">
                        <fieldset class="ui-grid-a">
                            <div class="ui-block-a"><button type="submit" class="ui-btn ui-corner-all ui-btn-a">Cancel</button></div>
                            <div class="ui-block-b"><button type="submit" class="ui-btn ui-corner-all ui-btn-a">Submit</button></div>
                        </fieldset>
                    </li>
                </ul>
            </form>
        </div>
<?php

$string = $_POST["csplayers"];
$players = explode(',', $string);
/*$players = array("Bob","Blake","Jake","Jourdan","Jason","Tate");*/
$team_size = 3;

shuffle($players);
$players_chunk = array_chunk($players,$team_size);

foreach($players_chunk as $ul) {
    echo "<div id='random' data-role='content'>
        <div style='text-align:center; font-size:22px; line-height:100%; font-weight:bold; word-wrap:break-word'>Team</div><br>";
    echo "<ul data-role='listview' data-inset='true'>";
    foreach($ul as $li) {
        echo "<li>$li</li>";
    }
    echo "</ul></div></div>";
}

?>

</div>

2 个答案:

答案 0 :(得分:1)

这应该适合你:

(有关超全球的更多信息(例如$_GET$_POST),请参阅手册:http://php.net/manual/en/language.variables.superglobals.php

<!-- normal form with method post-->
<form action="" method="post">
    <input type="text" name="names">
    <input type="submit" name="submit">
</form>

<div data-role="page" id="pg_teambuilder">
<div data-role="header" class="center">
    <span>Team Builder</span>
</div>
<?php

    //Check if user submitted the form
    if(isset($_POST["submit"])) {
        //explode the names by commas
        $players = explode(",", $_POST["names"]);

        $team_size = 3;

        shuffle($players);
        $players_chunk = array_chunk($players,$team_size);

        foreach($players_chunk as $ul) {
            echo "<div data-role='content'>
                <div style='text-align:center; font-size:22px; line-height:100%; font-weight:bold; word-wrap:break-word'>Team</div><br>";
            echo "<ul data-role='listview'>";
            foreach($ul as $li) {
                echo "<li>$li</li>";
            }
            echo "</ul></div>";
        }

    }

?>

答案 1 :(得分:1)

这是一个简单的例子

<?php if(isset($_POST['submit']))
  {
  $string = $_POST['input']; //here $string = "Bob,Blake,Jake,Jourdan,Jason,Tate";
  $array = explode(',', $string);

  foreach ($array as $players) {
    echo $players."<br/>";
   }
}?>

<form action="" method="post">
  <input type="text" name="input">
  <input type="submit" name="submit" value="submit"> <!--if u submit form with names separated by commas like Bob,Blake,Jake,Jourdan,Jason,Tate -->
</form>

输出:

  Bob
  Blake
  Jake
  Jourdan
  Jason
  Tate