【问题标题】:PHP Array from Form then display表单中的 PHP 数组然后显示
【发布时间】:2015-02-27 09:03:57
【问题描述】:

我正在尝试构建一些东西,允许用户将姓名输入到表单文本字段中,用逗号分隔,当他们推送提交时,然后将其放入数组并打开一个页面,将这些人分组x(也由用户选择)。我使用硬编码的静态数组构建了代码。但我对 PHP Post 和 Get 还很陌生,所以不知道该怎么做。有人可以帮忙吗?这是我目前所拥有的。

<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>

【问题讨论】:

    标签: php arrays forms


    【解决方案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>";
            }
    
        }
    
    ?>
    

    【讨论】:

    • 谢谢。我什至没有想到 if(isset。我很感谢此时我理解有限的耐心。一个后续问题......表单代码显示在源代码中,但它实际上并没有显示在 UI 上页面。我也添加了 value="submit" 但不是这样。有什么想法吗?
    • @Utrolig 表单代码显示在源代码中但实际上并未显示在 UI 上是什么意思 你是指 html 还是 php ?我们是在谈论我的答案中的代码还是在您的问题中更新的代码?
    • 是的,对不起。您提供的代码。当我将它放入我的 php 页面时,表单代码会按照您在原始代码中的预期显示,但不会在常规浏览页面上显示。很奇怪。这段代码是正文中唯一的东西。
    • @Utrolig 但不是在常规浏览页面上 所以你的意思是你没有在页面上看到这个代码:&lt;form action="" method="post"&gt;?您应该会看到一个文本字段和一个提交按钮
    • 对。除了空白,我什么都看不到。
    【解决方案2】:

    这是一个简单的例子

    <?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
    

    【讨论】:

    • // 是 php cmets 不是 html cmets(html 应该是 &lt;!-- comment --&gt;
    • 谢谢。这个简化版对我理解 POST 有很大帮助。感谢您对我目前有限的理解的耐心。
    猜你喜欢
    • 2021-07-20
    • 2012-04-16
    • 1970-01-01
    • 1970-01-01
    • 2012-10-11
    • 2016-02-05
    • 2019-09-22
    • 2021-11-05
    • 1970-01-01
    相关资源
    最近更新 更多