3

When clicking add row button, new row will add to the specific table. So I need to add a select option with php option values.

How to pass this php values to jQuery?

Jquery function I need to show select option inside the rowData.push('');

  $('.dt-add').each(function () {
        var whichtable = $(this).parents('form').attr('data-id');
        $(this).on('click', function(evt){
            //Create some data and insert it
            var rowData = [];
            var table = $('#teammembertable' + whichtable).DataTable();
//          rowData.push('');

            rowData.push('<select class="form-control addstafftype" id="addstafftype" name="addstafftype"><option value="">Select</option><option value="Leader">Leader</option><option value="Technician">Technician</option></select');
            rowData.push('<button type="button" data-id='+ whichtable +' class="btn-xs dt-delete dt-deletes"><i style="font-size:10px" class="fa">&#xf00d;</i></button>');
            table.row.add(rowData).draw( false );
        });
    });

PHP CODE

$dataadd_team_memb = array(
        'team_id' => $id,
        'Staff_id' => $this->input->post('getaddstaffname'),
        'Staff_type' => $this->input->post('getaddstafftype'),
        'status' => "active"
    );


    $insert_id = 0;
    if ($this->db->insert("team_members", $data)) {
        $insert_id = $this->db->insert_id();
    }
1
  • Put the select elements inside a form and submit that form to a PHP page. Commented Jul 31, 2019 at 7:50

1 Answer 1

2
$('.dt-add').each(function () {
    var whichtable = $(this).parents('form').attr('data-id');
    $(this).on('click', function(evt){
        var rowData = [];
        var table = $('#teammembertable' + whichtable).DataTable();
        rowData.push('<select class="form-control addstafftype" id="addstafftype" name="addstafftype">'+
            '<option value="">Select</option>'+
            '<?php foreach($selectallstaff as $staffname){ ?>'+
                '<option value="<?php $staffname["Staff_id"]; ?>"><?php $staffname["Staff_name"]; ?></option>'+
            '<?php } ?>'+
            '</select');
        rowData.push('<button type="button" data-id='+ whichtable +' class="btn-xs dt-delete dt-deletes"><i style="font-size:10px" class="fa">&#xf00d;</i></button>');
        table.row.add(rowData).draw( false );
    });
});
Sign up to request clarification or add additional context in comments.

4 Comments

its working now when i am giving <?= $staffname["Staff_id"]; ?>
remember also about <?= $staffname["Staff_name"]; ?>
Ya, when you use the latest version on PHP.
for($i=0;$i < count($addstaffname);$i++){ $addstaffname_final = $addstaffname[$i]; }

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.