douyong6585 2014-09-14 17:09
浏览 73
已采纳

表单字段需要一个数组

I will get right into it. I have an html page with a select form element tag named "location" and I coded a button to be able to add another select tag form field in case users need to indicate multiple different locations when submitting the form. My problem is I cannot seem to add an additional select form field with a different name="" ideally I would like name="location0" and when user clicks again to add another select form field it should be name="location1" and so on...

I tried the push() function with this code:

<button onclick="add_fields()">Try it</button>

<p id="demo"></p>

<script>
var location= ["location"];
document.getElementById("invoice").innerHTML = location;

function add_fields() {
    location.push("+")
    document.getElementById("invoice").innerHTML = location;
}
</script>

but that did not work either. My implementation of this code is the problem I'm sure.

I provided my code in a js fiddle, the script is included in the html section, only worked in the html section (weird), any help is appreciated, thx.

  • 写回答

2条回答 默认 最新

  • douzhi8244 2014-09-14 17:12
    关注

    Put "location[]" as the selects name and then when you receive the data via $_POST, that will be an array with the distinct values.

    If you put something like this, you will see that:

    <?php
    if ($_SERVER['REQUEST_METHOD']==='POST') {
        echo '<pre>';
        print_r($_POST);
        echo '</pre>';
        //$locationArray=$_POST['location'];
    
    }
    ?>
    

    $_POST['location'] will be an array with the distinct values in the distincts positions

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?