2011-01-06 8 views
0

일부 입력 텍스트 상자의 내용을 데이터베이스로 가져와야합니다. 첫 번째 상자가 표시됩니다 만 동적으로 그 중 일부를 만들려면이 자바 스크립트를 사용하고 있습니다 : 내가 PHP에서 그들을 참조 할 수 있습니다 내가 그렇게 만든 각 상자 다를 수 폼에 값을 추가 할 수있는 방법동적 양식에 값 추가

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++; 
    } 
} 

을 ?

감사합니다.

+0

확실하지를 당신이 원하는. 모든 입력 필드는'$ _POST [ 'myInputs']'배열을 통해 액세스 할 수 있습니다. –

답변

0

편집 : 입력을 그룹화하기 위해 배열을 사용 해본 적이 없지만 대신 사용할 수 있습니다. 이 같은 PHP에서 myInputs []하지만 참조로 입력의 이름을 유지 :

첫 번째 필드

등 두 번째에 대한

$_POST[myInputs][1] 

..

+0

빠른 답장을 보내 주셔서 감사합니다. 같은 줄을 따라 뭔가를 시도했지만 Javascript 구문에 익숙하지 않습니다. counter + 1을 어떻게 표현하여 2로 시작할 수 있을까요? – Sebastian

+0

왜 2시에 시작 하시겠습니까? – Calum

+0

첫 번째 상자가 이미 있고 자바 스크립트가 추가 된 상자를 충족시키기 때문입니다. 그래서 추가 된 첫 번째 것은 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>