2013-06-22 3 views
0

다음 폼이 'form'이라는 PHP 클래스 폼을 생성했습니다. 이 양식에는 여러 폼 요소를 동적으로 추가해야하는 섹션이 있습니다 (id = "pro_tasks"테이블).동적으로 PHP로 생성 된 폼 요소를 추가합니다

이 양식에서 사용자는 더 많은 작업을 추가 할 수 있어야합니다.

<?php 
     h2("Add Project"); 

     echo $form->create(array( 'id'  =>'addProject', 
            'name'  =>'addProject', 
            'method' =>'get', 
            'action' =>'index.php')); 

      $form->open_fieldset(); 
     onebreak(); 
      $form->legend("Project Detals"); 

     echo $form->text (array( 'id'  =>'pro_name', 
            'label'  =>'Project Name : ', 
            'name'  =>'pro_name', 
            'required' =>'required', 
            'length' =>255)); 
     twobreaks(); 
     echo $form->select(array( 'id'  =>'pro_type', 
            'label'  =>'Project Type : ', 
            'name'  =>'pro_type', 
            'data'  =>$form->option_array("SELECT DISTINCT id,pro_type 
                     FROM project_types", 
                     'id', 'pro_type'))); 
     twobreaks(); 
     echo '<label for="client">Client : </label> 
       <input type="radio" name="NA" value="company" checked="checked">Company 
       <input type="radio" name="NA" value="person">Person'; 
     echo '<div id="client">'; 
     //The client list generated inside this dive tag, based on the redio button selected above 
     //The Company list has selected by default 
     echo '<div id="companydiv">'; 
     echo $form->select(array( 'id'  =>'client', 
            'label'  =>' ', 
            'name'  =>'client', 
            'data'  =>$form->option_array("SELECT DISTINCT id, name 
                     FROM companies 
                     WHERE addressbook_id='1' OR addressbook_id='2' OR addressbook_id='3' OR addressbook_id='4'", 
                    'id', 'name'))); 
     echo '</div>'; 
     echo '</div>'; 

     twobreaks(); 
     echo $form->date (array( 'id'  =>'start_date', 
            'label'  =>'Start Date : ', 
            'name'  =>'start_date', 
            'required' =>'required', 
            'class'  =>'datepicker', 
            'length' =>255)); 
     bigspace(); 
     echo $form->date (array( 'id'  =>'comp_date', 
            'label'  =>'Completion Date : ', 
            'name'  =>'comp_date', 
            'required' =>'required', 
            'class'  =>'datepicker', 
            'length' =>255)); 

     twobreaks(); 
     echo $form->select (array('id'  =>'status', 
            'label'  =>'Project Status : ', 
            'name'  =>'status', 
            'data'  =>array('pending' =>'pending', 
                 'ongoing' =>'ongoing', 
                 'suspended' =>'suspended', 
                 'terminated' =>'terminated', 
                 'completed' =>"completed"), 
            'value'=>'pending')); 

     twobreaks(); 
     echo $form->select(array( 'id'  =>'resp_id', 
            'label'  =>'Responsibility', 
            'name'  =>'resp_id', 
            'data'  =>$form->option_array("SELECT DISTINCT id, CONCAT(first_name, ' (',user_type,')') AS resp_name 
                     FROM users 
                     WHERE status='ACTIVE'", 
                    'id', 'resp_name'))); 

     twobreaks(); 
     echo $form->select (array('id'  =>'priority', 
            'label'  =>'Priority : ', 
            'name'  =>'priority', 
            'data'  =>array('low'  =>'low', 
                 'moderate' =>'moderate', 
                 'high'  =>'high',), 
            'value'=>'low')); 

     twobreaks(); 
     echo $form->textBox(array ('id'  =>'description', 
            'label'  =>'Description : ', 
            'name'  =>'description', 
            'columns' =>'50', 
            'rows'  =>'5')); 

     twobreaks(); 
     echo $form->text(array ('id'  =>'tec', 
            'label'  =>'Total Estimated Cost (Rs) : ', 
            'name'  =>'tec')); 

     twobreaks(); 
     echo $form->textBox(array ('id'  =>'remarks', 
            'label'  =>'Remarks : ', 
            'name'  =>'remarks')); 
     twobreaks(); 

      $form->close_fieldset(); 

      $form->open_fieldset(); 
     onebreak(); 
      $form->legend("Project Tasks"); 
    ?> 
     <div class="overflowTable"> 
      <table id="pro_tasks"> 
       <tr align="center" class="FormTableHeader"> 
        <th>Task Name  </th> 
        <th>Start Date  </th> 
        <th>Completion Date </th> 
        <th>Responsibility </th> 
        <th>Status   </th> 
        <th>TEC    </th> 
        <th>Description  </th> 
       </tr> 
       <?php //Need some code to repeat the rows when this is a update form?> 
       <tr id="task"> 
        <td> <?php echo $form->text (array('id'  =>'task_name', 
                 'name'  =>'task_name[]', 
                 'required' =>'required'));?> </td> 

        <td> <?php echo $form->date (array('id'  =>'tsk_start_date', 
                 'name'  =>'tsk_start_date[]', 
                 'required' =>'required', 
                 'class'  =>'datepicker',));?> </td> 

        <td> <?php echo $form->date (array('id'  =>'tsk_comp_date', 
                 'name'  =>'tsk_comp_date[]', 
                 'required' =>'required', 
                 'class'  =>'datepicker',));?> </td> 

        <td> <?php echo $form->select(array('id'  =>'assignedto', 
                 'name'  =>'assignedto[]', 
                 'data'  =>$form->option_array("SELECT DISTINCT id, CONCAT(first_name, ' (',user_type,')') AS resp_name 
                          FROM users 
                          WHERE status='ACTIVE'", 
                          'id', 'resp_name')));?></td> 

        <td> <?php echo $form->select(array('id'  =>'tsk_status','name'=>'tsk_status[]', 
                 'required' =>'required', 
                 'data'  =>array('pending'  =>'pending', 
                      'ongoing'  =>'ongoing', 
                      'suspended'  =>'suspended', 
                      'terminated' =>'terminated', 
                      'completed'  =>'completed'), 
                 'value' =>'pending'));?> </td> 

        <td><?php echo $form->text (array('id'  =>'tec','name'=>'tec[]'));?> </td> 
        <td><?php echo $form->textBox(array('id'  =>'description','name'=>'description[]'));?></td> 
       </tr> 
      </table> 
     </div> 
     <div id="addProject_errorloc" class="error_strings"></div> 

     <img src="../../images/add.png" alt="addtask" id="addtask"> 
     <img src="../../images/remove.png" alt="removetask" id="removetask"> 

      <?php 
       $form->close_fieldset(); 
      echo $form->submit(array('id'=>'submit', 'name'=>'submmit'));     
      echo $form->close(); 
      ?> 

php로 생성 된 양식 요소는 자바 스크립트를 사용하여 동적으로 작업 행을 생성 할 수 없습니다. 그래서, 내가 작업 행을 생성 할 수있는 또 다른 PHP 페이지를 작성 JQuery와 같은 울부 짖는 방법을 GET 사용하여 양식에 추가

<?php require_once(dirname(__FILE__).'/../../../includes/initialize.php'); 
    $rowCount=""; 
    if(isset($_GET['rowCount'])){ 
    $rowCount = $_GET['rowCount']; 
    } 
    ?> 
<tr id="task"> 
<td> <?php echo $form->text (array('id'  =>'task_name', 
            'name'  =>'task_name['.$rowCount.']', 
            'required' =>'required'));?> </td> 

<td> <?php echo $form->date (array('id'  =>'tsk_start_date', 
            'name'  =>'tsk_start_date['.$rowCount.']', 
            'required' =>'required', 
            'class'  =>'datepicker',));?> </td> 

<td> <?php echo $form->date (array('id'  =>'tsk_comp_date', 
            'name'  =>'tsk_comp_date['.$rowCount.']', 
            'required' =>'required', 
            'class'  =>'datepicker',));?> </td> 

<td> <?php echo $form->select(array('id'  =>'assignedto', 
            'name'  =>'assignedto['.$rowCount.']', 
            'data'  =>$form->option_array("SELECT DISTINCT id, CONCAT(first_name, ' (',user_type,')') AS resp_name 
                     FROM users 
                     WHERE status='ACTIVE'", 
                     'id', 'resp_name')));?></td> 

<td> <?php echo $form->select(array('id'  =>'tsk_status','name'=>'tsk_status['.$rowCount.']', 
            'required' =>'required', 
            'data'  =>array('pending'  =>'pending', 
                 'ongoing'  =>'ongoing', 
                 'suspended'  =>'suspended', 
                 'terminated' =>'terminated', 
                 'completed'  =>'completed'), 
            'value' =>'pending'));?> </td> 

<td><?php echo $form->text (array('id'  =>'tec','name'=>'tec['.$rowCount.']'));?> </td> 
<td><?php echo $form->textBox(array('id'  =>'description','name'=>'description['.$rowCount.']'));?></td> 

$(document).ready(function(){ 
    $("#addtask").click(
     function(){ 
      var rowCount = $('#pro_tasks tr').length; 
      $.ajax({ 
       url: "task.php?rowCount="+rowCount, 
       type: "GET", 
       success: function(data){ 
        $("tbody").append(data);//Do something here with the "data" 
       } 
      }); 

     }); 
     }); 

코드의 모든 위의 (var에 rowCount를 제외하고는 제대로 작동 = $ ('# pro_tasks tr'). 길이;). 테이블의 행 수를 계산하지 않습니다. 따라서 동적으로 생성 된 행을 별도로 식별 할 수 없습니다.

모든 시간, 행 개수는 2 ...

내가이 분야의 전문가가 아니에요,이 문제에 나에게 조언을 충분하시기 바랍니다.

감사합니다.

+0

논리 오류가 아닌 원본 코드에 구문 오류가 있습니다. – Gajus

답변

3

var rowCount = $('#pro_tasks tr').length; 

당신이 (id를위한) 해시 서명을 잊어해야합니다.

+0

오, 미안하지만 질문을 쓰는 동안 실수를 했어. 이제 나는 그것을 고쳤다. 감사 – bhadrajith

1

나는 자바 스크립트에 캐싱이 있다고 생각한다.

그래서 URL에 rowCount를 보내는 대신 다음 코드를 사용했습니다. 문제 해결됨. :)

$(document).ready(function(){ 
     $("#addtask").click(
     function(){ 
      $.ajax({ 
       url: "task.php", 
       type: "GET", 
        success: function(data){ 
        $("tbody").append(data); 
      } 
     }); 

}); 
$('#submit').click(function(){ 
    $('#pro_tasks tr').each(function(index,item){ 
     $('input#task_name',item).attr('name','task_name['+index+']'); 
     $('input#tsk_start_date',item).attr('name','tsk_start_date['+index+']'); 
     $('input#tsk_comp_date',item).attr('name','tsk_comp_date['+index+']'); 
     $('select#assignedto',item).attr('name','assignedto['+index+']'); 
     $('select#tsk_status',item).attr('name','tsk_status['+index+']'); 
     $('input#tec',item).attr('name','tec['+index+']'); 
     $('textarea#description',item).attr('name','description['+index+']'); 
    }); 
}); 

});

양식을 제출할 때이 함수는 각 이름에 행 번호를 추가합니다.

감사합니다.

관련 문제