2011-01-06 23 views
0

一部の入力テキストボックスの内容をデータベースにプルする必要があります。最初のボックスが表示されますが、私は動的にそれらのいくつかを作成するには、このJavascriptを使用しています:動的フォームに値を追加する

var counter = 1; 
var limit = 10; 
function addInput(divName){ 
    if (counter !== limit) { 
      var newdiv = document.createElement('div'); 
      newdiv.innerHTML = " <input type='text' name='myInputs[]' size=40>"; 
      document.getElementById(divName).appendChild(newdiv); 
      counter++; 
    } 
} 

私はPHPでそれらを参照することができるように作成された各ボックスに異なるだろうフォームに値を追加することができますどのように?

ありがとうございます!

+0

わからない...あなたの最初のフィールドの

となど秒間

$_POST[myInputs][1] 

:[]しかし、このようなPHPで参照myInputsとしてあなたの入力の名前を保ちますあなたが欲しいもの。すべての入力フィールドは '$ _POST ['myInputs']'配列からアクセス可能です。 –

答えて

0

編集:入力をグループ化するために配列を使用しようとしたことはありませんが、代わりに使用します。

+0

クイック返信ありがとう、それはうまく動作します。私は同じ行に沿って何かを試していたが、私はJavascriptの構文に慣れていない。 counter + 1をどのように表現すれば2で始めることができますか? – Sebastian

+0

なぜ2で始めたいですか? – Calum

+0

最初のボックスはすでに存在しており、javascriptは追加されたボックスを処理するためです。追加された最初のものはt2になります。 – Sebastian

0
 <script src="//ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.js"></script>//use jQuery. it makes life easy. 
      <script>!window.jQuery && document.write(unescape('%3Cscript src="js/libs/jquery-1.4.2.js"%3E%3C/script%3E'))</script> 
      <script> 
       $(document).ready(function(){//fire when the page is ready 
        $('.addFields').click(function(){//any time someone clicks on a element with the class addFields 
         if($('.myInputs').length < 10){ // if there are less than 10 input fields... 
          $($(this).attr('rel')).append("<br/><input type='text' name='myInputs[]' class='myInputs' size='40' id='myInputs"+($('.myInputs').length)+"'>"); 
          // add another input field with an ID equal to it's place in line. 
           } 
        }); 
       }); 
      </script> 

     </head> 
     <body > 
      <a rel="#addToMe" class="addFields" href="#">Add a field</a><!-- the triggering element this can be anything that can be clicked on--> 
      <div id="addToMe"><!-- the element holding our input fields, new ones get added to the back of here, note that the trigger's rel attribute is the ID of this attribute and started with a "#' ID identifier--> 
       <input type='text' name='myInputs[]' class='myInputs' size='40' id='myInputs0'> 
      </div> 

     </body>