0

I have a javascript which adds text boxes when clicked, I tried to submit the data but it can only detect the original textbox

Here is the code of javascript

  var textbox = document.createElement('input');
  textbox.setAttribute('type', 'text');
  textbox.setAttribute('name','description[]');
  textbox.setAttribute('id', 'tasks');
  textbox.setAttribute('class', 'form-control form-control2');
  textbox.setAttribute('required', 'true');
  textbox.setAttribute('placeholder', 'Describe Problem Encountered...');
  document.getElementById('appendtb').appendChild(textbox);

Here is a part of code in html

 <tr>
     <td colspan="6" id="appendtb">
         <!-- this is the textbox that it detects-->
         <?php echo form_input(['name'=>'description[]','placeholder'=>'Describe Problem Encountered...','class'=>'form-control form-control2','required'=>'true','id'=>'task']); ?>
      </td>
 </tr>
 <tr>
     <td colspan="3">
         <input type="button" id="addTask" onclick="addTaskfield()" class="btn btn-outline-primary" name="btnAddTask" value="Add Task"/> 
     </td>
     <td colspan="3" align="right">
         <?php echo form_submit(['name'=>'submit','value'=>'Submit Request','class'=>'btn btn-outline-success btn-submitC']); ?>
     </td>
 </tr>

Here is the PHP code

$description = $this->input->post('description');
print_r($description);
exit();
13
  • You don't have to use .setAttribute() to set properties of an element node. You can just write textbox.type = "text"; etc. Only weird thing is class, for which you have to use textbox.className = "whatever";. Commented Mar 22, 2018 at 13:00
  • 1
    That said, your code repeats the same "id" value with every element, which is not a good idea. The values of "id" attributes should be unique across the whole document. Commented Mar 22, 2018 at 13:02
  • @Pointy - The specs even say that id's must be unique within a document. Commented Mar 22, 2018 at 13:05
  • ok thanks for the help :) Commented Mar 22, 2018 at 13:13
  • but when i hit submit it can only post the first textbox Commented Mar 22, 2018 at 13:14

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.