0

I want to display the form with user filled values inside the form to admin. I have displayed all type of values to the respective type of input/select/textarea tags (type: text,email, tel,number... etc) except input type=checkbox.

I am getting problem while fetching the values from array that contain values of group of checkboxes. My code is

                    var value = data[i][key];
                var result = $.isArray(value);
                if (result == true) {
                    var string = key;
                    var splitstring = string.split("#");
                    for (var value1 in value) {
                        console.log(value1);
                        $("input[type='checkbox'][groupid='" + splitstring[0] + "'][value='" + value1 + "']").attr('checked', true);  //cb
                    }
                }

my array(named value) contain values like [cricket, football, tennis]

would like to make the checkbox property checked that match the condition. but when i console the values fetched one by one it shows me output as 0 1 2

i am not getting what is it???

my html code

                <table class="form-group">
                <tbody id="tedit">
                    <tr>
                        <td>
                            <div class="checkbox">
                                <input id="dddddddd#1428735544884535#check_box1" class="form-control" name="14287355448849394#dddddddd[]" groupid="14287355448849394" grid-name="dddddddd" value="Check Box1" type="checkbox" /><label class="jedit"><span class="mouseover">Check Box1</span></label>
                            </div>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <div class="checkbox">
                                <input id="dddddddd#14287355448843282#check_box2" class="form-control" groupid="14287355448849394" grid-name="dddddddd" name="14287355448849394#dddddddd[]" value="Check Box2" type="checkbox" /> <label class="jedit"> <span class="mouseover">Check Box2</span></label>
                            </div>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <div class="checkbox">
                                <input id="dddddddd#14287355448853367#check_box3" class="form-control" groupid="14287355448849394" grid-name="dddddddd" name="14287355448849394#dddddddd[]" value="Check Box3" type="checkbox" /> <label class="jedit"> <span class="mouseover">Check Box3</span></label>
                            </div>
                        </td>
                    </tr>
                </tbody>
            </table>

my javascript code is

$.post('<?php echo BASE_URL . 'php/processing/formDashboard/formEntryShowOneByOne.php' ?>', {id: $('#formMetaDataId').val()}, function(data) {
        console.log(data);
        for (var i = 0, len = data.length; i < len; i++) {
            for (var key in data[i]) {
                $("input[type='text'][name='" + key + "']").val(data[i][key]); //input tags
                $("input[type='text'][name='" + key + "']").prop('disabled', 'true'); //input tags

//........likewise for other type of elements.......///

//.....................for checkbox........................//
                var value = data[i][key];
                var result = $.isArray(value);
                if (result == true) {
                    var string = key;
                    var splitstring = string.split("#");
                    for (var value1 in value) {
                        console.log(value1);
                        $("input[type='checkbox'][groupid='" + splitstring[0] + "'][value='" + value1 + "']").attr('checked', true);  //cb
                    }
                }

            }
        }
    });
2
  • Post your form html code and full loop code. Commented Apr 11, 2015 at 7:57
  • well the html is too big so i have posted only the checkbox part Commented Apr 11, 2015 at 8:02

3 Answers 3

1

this is simple. The 'cricket' string is retrievable like this:

value[value1]

value1 is just the iterator, in your example 0,1,2

This is your working code:

var value = data[i][key];
var result = $.isArray(value);
if (result == true) {
    var string = key;
    var splitstring = string.split("#");
    for (var value1 in value) {
        console.log(value[value1]);
        $("input[type='checkbox'][groupid='" + splitstring[0] + "'][value='" + value[value1] + "']").attr('checked', true);  //cb
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

oh! i that is the way. i supposed to thing the loop as of PHP foreach loop. Thanks i accept this ans
0

You should use forEach function

value.forEach(function(val)
{
  console.log(val);
  //do your thing here
});

Comments

0

That's just how the for ... in loop works for arrays:

var a = ['a','b','c'];
for (var i in a) {
  alert(i); // alerts 0,1,2
  alert(a[i]); // alerts a,b,c
}

Thus, you just need to index your array with the loop variable:

var value = data[i][key];
var result = $.isArray(value);
if (result == true) {
    var string = key;
    var splitstring = string.split("#");
    for (var valueIndex in value) {
        console.log(value[valueIndex]);
        $("input[type='checkbox'][groupid='" + splitstring[0] + "'][value='" + value[valueIndex] + "']").attr('checked', true);  //cb
    }
}

Comments

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.