【问题标题】:Read table and output via echo通过 echo 读取表和输出
【发布时间】:2017-06-25 14:54:19
【问题描述】:

在我的第一段代码中,您可以看到我如何使用回显输出构建表格。一切正常。

 echo "<form method=\"post\" action=''>";
    echo "<table border=\"1\">";
    foreach($pdo -> query($sqlSpielerNamen) as $row){
        echo "<tr>";
        echo "<td style=\"font-size: 10;\">".$row['Player_ID']."</td>";
        echo "<td style=\"font-size: 10;\">".$row['SpielerName']."</td>";
        echo "<td syle=\"font-size: 10 ;\"><input name='note' type='text' class='textfield' id='note_id' value='$spielID' />";
        echo "<td syle=\"font-size: 10 ;\"><input name='tore' type='text' class='textfield' id='tore_id' value='$spielID' />";
        echo "</tr>";
    }
    echo "</table>";
    echo "<br><br><input type=\"submit\" name=\"submit_eingabemaske\" value=\"Abschicken\">\n";
    echo "</form>";

但是现在,我需要在单击提交按钮后从此表中获得新的输出。 我需要第一个字段“Player_ID”和最后两个字段“note”和“tore”。

目前我的输出看起来像这样。但我知道这不是正确的解决方案。

if (isset($_POST['submit_eingabemaske'])){
$Po = $_POST["tore"];
foreach ($row as $item){
            echo $Po;
}}

我必须在第二段代码中做些什么,我的结果才会正确。我怎样才能做到这一点?我的另一个问题是,如何调用第一个字段“Player_ID”?我可以使用 $POST["tore"] 获得的字段 'tore' 和 'name'...

【问题讨论】:

  • name='note' 应该是 name='note[]' 并且对于其他输入也是如此,然后遍历该数组并输出。还要对所有 HTML 属性使用单引号。 ID 也应该是唯一的,如果值相同,您实际上不需要第二个输入,对吧?

标签: php html-table echo


【解决方案1】:

这里有很多初学者的错误。

首先 - 您不应该使用带双引号的复杂字符串。

还有更简单的方法:

// see, I wrap string in single quotes, 
// and all quotes inside the string are double ones
echo '<form method="post" action="">';

更简单的是关闭?&gt;:

<?php
// code here
?>
<form method="post" action="">

下一步,当您创建许多具有相同name 的字段时,您将只收到具有该名称的字段的最后一个值。所以,

<input name='note' type='text' class='textfield' id='note_id' value='11' />
<input name='note' type='text' class='textfield' id='note_id' value='22' />
<input name='note' type='text' class='textfield' id='note_id' value='33' />

只会在服务器端给你33。这就是为什么您需要为name 使用[] 表示法:

<input name='note' type='text' class='textfield' id='note_id[]' value='11' />
<input name='note' type='text' class='textfield' id='note_id[]' value='22' />
<input name='note' type='text' class='textfield' id='note_id[]' value='33' />

在这种情况下,您的 $_POST['note_id'] 将保存表单中的所有值。 与name='tore'相同。

第三,如果你想将一些隐藏的值传递给服务器,你可以使用input 类型为hidden,它在页面上看不到但仍然存在并传递给服务器:

<!-- Again, use [] for getting several values -->
<input type="hidden" name="player_id[]" id="" value="<?=$Player_ID?>" />

因此,您可以将代码重写为:

<?php
// Some code here
// close previous opened php-tag
?>
<form method="post" action=''>
<table border="1">
<?php
foreach($pdo -> query($sqlSpielerNamen) as $row){?>
    <tr>
        <td style="font-size: 10;"><?php echo $row['Player_ID']?><input type="hidden" name="player_id[]" value="<?=$row['Player_ID']?>" /></td>
        <td style="font-size: 10;"><?=$row['SpielerName']?></td>
        <td style="font-size: 10 ;"><input name='note[]' type='text' class='textfield' value='<?=$spielID?>' />";
        <td style="font-size: 10 ;"><input name='tore[]' type='text' class='textfield' value='<?=$spielID?>' />";
    </tr>
<?php
}?>
</table>
<br><br><input type="submit" name="submit_eingabemaske" value="Abschicken">
</form>

这里的&lt;?=&lt;?php echo 的简短语法。

在服务器上你可以:

if (isset($_POST['submit_eingabemaske'])){
    print_r($_POST);    // see what you have in post
    foreach ($_POST['player_id'] as $id){
        // do something
    }
}

最后一个警告是使用inputid 属性。

id 属性必须在页面上是唯一的。您可以拥有相同的 id,但使用这些 id 的 所有 javascript 代码仅适用于具有此 id 的第一个元素。这就是我从输出中删除id 的原因。如果您需要所有输入的共同点 - 使用类。

更新:在我看来,如果你想用foreach生成文件,你可以:

// define array of fields' params
$fields = [
    ['name' => 'player_id', 'value' => $row['Player_ID'], 'type' => 'hidden', 'class' => ''],
    ['name' => 'note', 'value' => $spielID, 'type' => 'text', 'class' => 'textfield'],
    ['name' => 'tore', 'value' => $spielID, 'type' => 'text', 'class' => 'textfield'],
];
foreach ($fields as $f) {
    echo '<input name="' . $f['name'] . '[]" type="' . $f['type'] . '" class="' . $f['class'] . '"  value="' . $f['value'] . '" />';
}

【讨论】:

  • 感谢您提供的出色解决方案。但我有最后一个问题。在第二段代码中,此刻有 von field player_id。但是 3 个字段的 foreach 会如何... player_id、note、torre?我想在一行中回显所有三个字段
  • 我建议你写下你想看到的最终结果,然后考虑如何转移到foreach - 看看这些字段有什么共同点,有什么变化。但无论如何,我认为这不是一个好主意,因为有许多不同的项目应该以某种方式进行迭代。
  • 我是绝对的初学者,目前我正在尝试学习 php。我想要这三个字段的输出。目前,这对我来说是否有意义并不重要。我只想尝试一些东西......
猜你喜欢
  • 2018-04-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多