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();
Ace
  • 1
  • 3
  • 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";`. – Pointy Mar 22 '18 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. – Pointy Mar 22 '18 at 13:02
  • @Pointy - The specs even say that id's _must_ be unique within a document. – M. Eriksson Mar 22 '18 at 13:05
  • ok thanks for the help :) – Ace Mar 22 '18 at 13:13
  • but when i hit submit it can only post the first textbox – Ace Mar 22 '18 at 13:14
  • first textbox is called description, the rest are description[] been a whils since I did php but think that may make a difference when trying to get the data in the post or get. – Pete Mar 22 '18 at 13:19
  • ok ok i tried adding brackets but still it can't get the data's from the added textboxes – Ace Mar 22 '18 at 13:21
  • can you show your php where you try to read them - does it look like `$_POST['description'][0]` – Pete Mar 22 '18 at 13:27
  • @Pete i updated my post, i added the PHP code, TIA – Ace Mar 22 '18 at 13:40
  • did you try `$description[0]` what was in your print_r - not sure how code ignitor deals with posted vars or this may help: https://stackoverflow.com/questions/3112690/getting-data-from-post-array-in-codeigniter – Pete Mar 22 '18 at 13:55
  • it displays Array ( [0] => TEST ) – Ace Mar 22 '18 at 14:21
  • do you use ajax or not? – plonknimbuzz Mar 22 '18 at 15:28
  • no, i used php with codeigniter – Ace Mar 23 '18 at 01:13

0 Answers0