1

This time I wrote html in controller when I check a checkbox I get data in console but I want to get it in the form.

I have checkbox in foreach loop

<input type='checkbox' value='{$student['id']}' name='student_ids[]' id='chkbx' required> {$student['name']}

when click on checkboxes data load accordingly

its my html

    $str .= '<div class="form-group">
    <label class="control-label col-md-3">
    Student
    </label>
    <div class="col-md-4">
        <select id="students-clicked" class="form-control select2-multiple" multiple name="students[]">
            <option value=""></option>';
            foreach ($students as $students) {
                $str .= "<option value='{$student->id}'>{$student->name}</option>";
            }

$str .=         '</select>
    </div>
</div>';

Jquery Method is as

    $('body').on('click', "#chkbx", function() {
    var class = $('input:checkbox:checked').map(function() {
        return this.value;
    }).get();
    $.ajax({
            url: '/trigger/studentsData',
            type: 'POST',
            data: {'trigger_type': 'click' , ids: class},
            success: function(result) {

            }
        });
});

Except jQuery everything is written in controller this time I am getting HTML data in console how can I append it in my html form it should also update according to checkbox selection

5
  • Dont write html in controller. Commented Jun 6, 2017 at 9:53
  • @Kyslik it requirement here in my case that'ts why i am using this approach Commented Jun 6, 2017 at 9:54
  • you want to show your html after checkboxes? Commented Jun 6, 2017 at 9:57
  • yes above html in my question changes on chekboxes selection i want to show that div in my form Commented Jun 6, 2017 at 9:59
  • see my answer @bluemoon Commented Jun 6, 2017 at 10:01

2 Answers 2

1

Try this :-

<input type='checkbox' value='{$student['id']}' name='student_ids[]' id='chkbx' required/> {$student['name']}
<div id="appendata">
 //your ajax data
</div>

Now in Success Function

success: function(result) {
    $("#appendata").html(result); // this will replace when you select the checkbox
         or
    $("#appendata").append(result); // this will append when you select again from checkbox
}

Hope it helps

Sign up to request clarification or add additional context in comments.

6 Comments

its close to solution but not exact its insert 5 html fields if i check 5 checboxes it should give all data in one dropdown
if i click 2nd checkbox it should insert data in same field not need to insert new
then you have to do changes in your html that is to be used in controller. i have give answer according to your problem now you have to look your code
then use html(result);
thanks +1 for help please also vote question if you think it should :)
|
1

Don't append() rather use html() to override the content with the response data

success: function (result) {
    $("#container").html(result);
}

Plus, if you have more than one checkboxes, don't use id, rather use class attribute to loop through them

1 Comment

thanks +1 for help please also vote question if you think it should :)

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.