【问题标题】:How can I pass a list of elements from html to php? [duplicate]如何将元素列表从 html 传递到 php? [复制]
【发布时间】:2019-05-29 04:27:51
【问题描述】:

我有一个这样写的 html 列表:

<form action="check.php" style="display:none" method="post">
   <h1 class="text-center">your items are</h1>
   <ul id="riepilogo" name="riepilogo">
       <br/>
       <li name="">first item</li>
       <li name="">second item</li>
       <li name="">third item</li>
       ...
   </ul>

   <input type="submit" value="Pay" id="pay"/>
</form>

这段代码写在一个html页面中,现在的问题是我不知道如何使用php在新页面中显示所有这些项目。我认为也许我需要为列表中的所有项目设置相同的名称,然后将它们存储在 php 数组中。我真的不知道!也许它不能用php,我真的不知道。 感谢丹尼尔的帮助

【问题讨论】:

  • 您的 html 无效 - &lt;ul&gt; 和 &lt;h1&gt; 99% 的时间不应包含在 &lt;form&gt; 中
  • 可以查看这个问题的答案Pass form data to another page with php
  • @treyBake 在&lt;form&gt; 中使用&lt;ul&gt; 在技术上是完全有效的,所以不要另外声明。它不会发布数据,但这不会使其成为无效的 html。
  • @PeterB 啊,是的,抱歉,我认为我使用了错误的术语!我的意思是,它是不正确的语义 - 不是无效的HTML
  • @PeterB 或者我脑子里有一个印象,那就是做 xD 是 100% 不正确的

标签: html


【解决方案1】:

你需要给每个li加上[]的名字来作为一个数组提交

     <form action="check.php" style="display:none" method="post">
   <h1 class="text-center">your items are</h1>

   <ul id="riepilogo" name="riepilogo">
       <br/>
       <li >first item <input type='hidden' name='riepilogo[]' value='first item'/></li>
       <li >second item <input type='hidden' name='riepilogo[]' value='second item'/></li>
       <li >third item <input type='hidden' name='riepilogo[]' value='third item'/></li>
       ...
   </ul>

   <input type="submit" value="Pay" id="pay"/>
</form>

提交后,您将在 $_POST 中获得一个名为 riepilogo 的数组,其中包含这些值。

【讨论】:

  • 仅&lt;input&gt;、&lt;select&gt; 和&lt;textarea&gt; 将数据发布到&lt;form&gt;。
  • 啊是的我忘记输入是的现在检查
猜你喜欢
  • 2015-02-28
  • 1970-01-01
  • 1970-01-01
  • 2021-08-16
  • 1970-01-01
  • 2017-01-09
  • 2016-03-02
  • 2021-05-18
  • 1970-01-01
相关资源
最近更新 更多