duanliyi5997 2018-06-12 18:50
浏览 83

使用javascript生成的HTML文本输入标记在提交表单时不会POST

I have a form which asks for employment history, where you have applied to college etc. So for example for colleges they applied to I have one text box at first and there is a button below that calls a javascript function to add another text input right below it. When I first made this form I was doing that with a few different pieces of data then once they were submitted I would get them from $_POST and put them in arrays then add each element of the array to the corresponding table in my db. All of a sudden though, I can no longer submit my form and I get a message telling me that I have tried to get an unspecified index. However when i inspect the text inputs in my browser they are correctly named. I read that I should name them college[] so that they all go into an array, but that also did not work.... now what?

js:

var numcol = 1;
function addnewschool(){
 numcol++;
 var container = document.getElementById("collegecontainer");
 container.appendChild(document.createTextNode(numcol));
 var input = document.createElement("input");
 input.type = "text";
 input.name = 'applied'+numcol;
 container.appendChild(input);
 container.appendChild(document.createElement("br"));}

html:

<p class="text-dark mb-4">List the Colleges you have applied to:<br>
<div id="collegecontainer" name="collegecontainer">
<input type="text" name='applied1'><br>
</div>
<input type="button" id="addcollege" name="addcollege" value="Add College" 
onClick="addnewschool()"><br>
</p>

php:

$applied = array();
foreach($_POST['applied'] as $value){
  array_push($applied, $value);
}

Update: Ok so I changed it to another way and that didn't work so i decided to just copy an earlier version of it which was working and paste it into the correct layout. And that worked... SO now that it was fixed i continued adding to it. I added "required" to a few tags, changed some styling a bit, and changed my javascript file a bit. But now I'm having the same issues as before. All of my POST arrays only have the first value in them. What could I have added that changed this? Has anyone ever had any issues like this?

  • 写回答

3条回答 默认 最新

  • dozc58418381 2018-06-12 18:53
    关注

    Your first additional input should have index "applied1", not "applied2"

    look at:

    var numcol = 1; // put this to 0
    function addnewschool(){
    numcol++; // or move this to the end of the function
    
    评论

报告相同问题?

悬赏问题

  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集
  • ¥15 在启动roslaunch时出现如下问题
  • ¥15 汇编语言实现加减法计算器的功能
  • ¥20 关于多单片机模块化的一些问题
  • ¥30 seata使用出现报错,其他服务找不到seata
  • ¥35 引用csv数据文件(4列1800行),通过高斯-赛德尔法拟合曲线,在选取(每五十点取1点)数据,求该数据点的曲率中心。
  • ¥20 程序只发送0X01,串口助手显示不正确,配置看了没有问题115200-8-1-no,如何解决?
  • ¥15 Google speech command 数据集获取