簡體   English   中英

如何動態創建表單輸入,然后將其提交給php

[英]How to dynamically create form inputs and then submit them to php

我有一個頁面,用戶可以在其中按一個按鈕,該按鈕會將兩個新的表單輸入追加到該頁面。 用戶可以根據需要多次執行此操作。 我遇到的問題是我似乎無法訪問要提交的php文件中的新輸入。

這是用戶看到的頁面上按鈕的代碼。 它在表格內。

<div class='instruction'> 
    <p>Please use the checkpoint tool to outline the primary steps 
       necessary for completion of the project.
       <h4 style='margin-bottom:5px;'>Checkpoint Tool</h4>
       <input type='button' onclick='addCheckpoint()' value='Add Checkpoint' />
       <input type='text' id='count' name='projectCount' style='width:25px;' readonly='true' />
    </p>
    <div id='checkpoints'> 
    </div> 
</div> 

這是JavaScript函數addCheckpoint()

function addCheckpoint()
{     

  var count = +document.getElementById('count').value; 

  //  Declare Variables

  var checkpointText = "Checkpoint "+(count+1); 
  var nameText = "Checkpoint Name: "; 
  var instructionText = "Instructions: ";

  var previous = document.getElementById("checkpoints");

  var newP = document.createElement("p");

  var nameInput = document.createElement("input");
      nameInput.setAttribute("type","text"); 
      nameInput.setAttribute("name","checkpointName" + count);



  var instructionInput = document.createElement("textarea");
      instructionInput.setAttribute("name","checkpointInstruction" + count);
      instructionInput.setAttribute("rows","4");
      instructionInput.setAttribute("cols","56");

  //  Append Variables 

      newP.appendChild(document.createTextNode(checkpointText));
      newP.appendChild(document.createElement("br"));
      newP.appendChild(document.createElement("br"));
      newP.appendChild(document.createTextNode(nameText));
      newP.appendChild(nameInput);
      newP.appendChild(document.createElement("br"));
      newP.appendChild(document.createTextNode(instructionText));
      newP.appendChild(instructionInput);
      newP.appendChild(document.createElement("br"));
      newP.appendChild(document.createElement("hr"));

      previous.appendChild(newP);


      document.getElementById('count').value = count + 1; 
}     

這是提交到頁面上的代碼,該代碼試圖檢索發布的值。

$projectCount = strip_tags($_POST["projectCount"]); 
     for($i = 0; $i < $projectCount; $i += 1)
    {   
       $checkpointNames[] = strip_tags($_POST["checkpointName".$i]);
       $checkpointDescriptions[] = strip_tags($_POST["checkpointDescription".$i]);           
    } 

感謝提供的任何幫助!

好的,我知道了,這是一個菜鳥錯誤。 我的輸入甚至沒有附加到我的表單中。 謝謝您的幫助! 感謝您抽出寶貴的時間查看我的代碼。

嘗試將元素放入中,並確保使用提交按鈕或javasrtipt的form.submit提交表單

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM