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 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟