简体   繁体   English

带帖子的表单中的HTML动态元素数

[英]HTML dynamic number of elements in a form with post

I have code that basically uploads a spreadsheet of data, could have 5 columns or 10 columns or whatever. 我的代码基本上可以上传电子表格数据,可以有5列或10列或其他内容。 Each column needs to have a drop down box specifying the data type, and the data types selected should be submitted via post with a form to another php site. 每列都需要有一个指定数据类型的下拉框,并且所选数据类型应通过带有表单的表单提交到另一个php站点。 How would I do this, this is the code that creates the elements.Is there a way to dynamically create elements, like delimist . 我该怎么做,这是创建元素的代码。是否有一种动态创建元素的方法,例如delimist。 attCount, and how would i put this loop into an html form to submit the data? attCount,我如何将此循环放入html表单中以提交数据? Is there any way to put the values of all of these into an array for convince? 有什么方法可以将所有这些值放入数组以说服? Thanks. 谢谢。

EDIT: Ok thanks for the dynamic stuff, but how would i put this into a form and submit this via post to another page. 编辑:好的,感谢动态的东西,但是我如何将其放入表单并将其通过帖子提交到另一个页面。 Or is there another way to post information. 还是有另一种发布信息的方式。 I dont know much about php. 我不太了解php。

    while($attCount < count($attArray)) {
        echo "<select name=\"delimList\">";
        echo "<option value=1>tab</option>";
        echo "<option value=2>comma</option>";
        echo "<option value=3>semi-colon</option>";
        echo "<option value=4>ampersand</option>";
        echo "<option value=5>pipe</option>";
        echo "</select>";
        $attCount = $attCount + 1;
     }

You can put all the value of all of these into an array… by putting all the values of these into an array! 您可以将所有这些值放到一个数组中……通过将所有这些值放到一个数组中!

$myArray = ['tab','comma','semi-colon','amperstand','pipe','another-value'];
echo '<select name="delimList">';
for ($i = 0; $i < count($myArray); $i++) {//Loop thru all
    echo '<option value="'.$i.'">'.$myArray[$i].'</option>';
}
echo "</select>";

Using a foreach loop and assigning the first index in your array will give you what you're expecting: 使用foreach循环并在数组中分配第一个索引将为您提供期望的结果:

$options = [1=>'tab','comma','semi-colon','amperstand','pipe','another-value'];
echo "<select name=\"delimList[]\">\r\n";
foreach($options as $val => $option){
    echo "<option value=\"$val\">$option</option>\r\n";
}
echo "</select>\r\n";

output: 输出:

<select name="delimList[]">
<option value="1">tab</option>
<option value="2">comma</option>
<option value="3">semi-colon</option>
<option value="4">amperstand</option>
<option value="5">pipe</option>
<option value="6">another-value</option>
</select>

To sumbit the page you'll need to put everything inside of 要汇总页面,您需要将所有内容放入其中

<form method="post"> // you may want to add an action="..."
....                 // if you want to post to another page,
</form>              // instead of the same one

You'll also need a submit button too... 您还需要一个提交按钮...

<input type="submit" name="submit" value="submit" />

After submit you can check the values like this: 提交后,您可以检查以下值:

if(isset($_POST['delimList'])){
    foreach($_POST['delimList'] as $key => $value){
        echo "delimList[$key] = $value";
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM