2011-10-23 30 views
1

我有一個頁面,用戶可以按下一個按鈕,將兩個新的表單輸入添加到頁面。用戶可以根據需要多次執行此操作。我遇到的問題是我似乎無法訪問我正在提交的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]);   
    } 

我很感激任何可以提供的幫助!

+0

如果您在PHP中添加了一些調試功能,比如將$ _POST轉儲到屏幕上......它看起來如何? – lampej

+0

我在$ _POST上使用了var_dump,它打印出我的表單中除動態創建的表單外的所有輸入。同樣,當我嘗試使用GET而不是POST時,創建的輸入不會提交到URL。 – user1009142

回答

0

嘗試把要素引入,並確保你有一個提交按鈕或form.submit從javasrtipt

+0

我正在使用提交按鈕。 – user1009142

1

好提交表單,我想通了,這是一個新手的錯誤。我的輸入甚至沒有被添加到我的表單中。感謝您的幫助!我很欣賞你給我查看代碼的時間。

+0

您應該添加答案,而不是此評論。 –

+0

我做到了。我的代碼工作,但問題是生成的輸入不被附加到表單。這就是爲什麼當我提交表格時,他們不包括在內。 – user1009142

相關問題