1

I've a dynamic table which adds more rows. Here is my table.

<table> 
  <thead>
    <tr class="success">
     <th>Bank/Lender</th>
     <th>Add More</th>
    </tr>
  </thead>
  <tbody >
   <tr>
    <td>
     <?php
       $data = array(
              'name' => 'debt_info_bank_name[]',
              'id' => '',
              'class' => 'form-control',
              'placeholder' => 'Bank/Lender Name'
              );
       echo form_input($data);
      ?>
    </td>
    <td>
     <input class="add btn btn-success btn-sm" type="button" value="Add More"  />
    </td>
  </tr>
</tbody>

And Here is my javascript part:

    $(document).on('click','.add',function(){ 
    $(this).val('Delete');
    $(this).attr('class','del btn btn-danger btn-sm');
    var appendTxt = '<tr><td><?php  $data = array(
                                    'name' => 'debt_info_bank_name[]',
                                    'id' => '',
                                    'class' => 'form-control',
                                    'placeholder' => 'Bank / Lender Name'
                                  );
                                  echo json_encode(form_input($data)); ?></td><td><input class="add btn btn-success btn-sm" type="button" value="Add More"  /></td></tr>';
    $("#dyn_table > tbody > tr:last").after(appendTxt);
    });

    $(document).on('click','.del',function(){ 
        $(this).parent().parent().remove();

    });

It adds rows but with quotes"" in each td element. How can i correct it?

Note: If i add fields in javascript code using html elements like

 var appendTxt = '<tr><td><input type="text" placeholder="Bank/Lender Name"  /></td> <td><input class="add btn btn-success btn-sm" type="button" value="Add More" /></td></tr>';

then it goes fine but i want to add fields in codeigniter style because it is more safe to do that.

5
  • Why do you use json_encode? Just echo form_input($data); should do the trick? Commented Jul 12, 2016 at 12:02
  • removing json_encode leads to javascript error: " SyntaxError: unterminated string literal ". Actually it adds extra line break before closing of </td> thats why it gives error. @Mudshark Commented Jul 12, 2016 at 12:07
  • I would look at the script in the browser, after the form_input has been inserted. json_encode is definitely the wrong solution to the syntax error problem. Commented Jul 12, 2016 at 23:18
  • can you please come up with solution for me. @Jerry Commented Jul 14, 2016 at 12:28
  • I really can't, not without looking at the code as it's rendered in the browser. Remove the json_encode and then view the actual html using your browser's inspector. to get rid of extra whitespace around the generated input, use php's trim() method. But I think you will find that's not the actual problem. Commented Jul 14, 2016 at 17:28

1 Answer 1

1

remove json_encode from here. just echo

 echo form_input($data); ?></td><td><input class="add btn btn-success btn-sm" type="button" value="Add More"  /></td></tr>';
Sign up to request clarification or add additional context in comments.

1 Comment

this project is closed and dusted it too. but thanks.

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.