0

When I click on buttons I generate some inputs field with custom attributes like that :

<input type='text' name='field["+ i++ +"]' value='' data-kind='title' />
<input type='text' name='field["+ i++ +"]' value='' data-kind='video' />
<input type='text' name='field["+ i++ +"]' value='' data-kind='text' />

I retrieve the 'name' value with a foreach loop in PHP :

$result = array_combine($num, $records);

    foreach ($result as $rank => $content)
    {
        $data = array(
            'content' => $content,
            'post_id' => $post_id,
            'rank' => $rank,
            'type' => $this->input->post('field_type') // HERE
            );
                echo '<pre>';print_r($data);echo '</pre>';
    }

To get the 'type' I do a $this->input->post('field_type'); which is given by this :

var field_type = $(":input[data-kind]").attr('data-kind');
$("#field_type").val(field_type' ');

and :

echo '<input type="hidden" id="field_type" name="field_type" value="" />';

But it only returns me the last 'data-kind' value not each one :/

Now i just need to loop the 'data-kind' value for each input fields and retrieve them in my foreach loop

Any help would be very very appreciated !!


Many thanks for your answers, it helped me a lot! But now how can I add the result in my current foreach at 'type' data :

$result = array_combine($num, $records);

    foreach ($result as $rank => $content)
    {
        $data = array(
            'content' => $content,
            'post_id' => $post_id,
            'rank' => $rank,
            'type' => // HERE I NEED EACH ATTRIBUTE VALUE
            );
                echo '<pre>';print_r($data);echo '</pre>';
    }

3 Answers 3

1

If you want to place all of the data-kind values in the #field_type field, you need something like this:

var fieldTypes = [];
$("input[data-kind]").each(function()
{
    fieldTypes.push( $(this).attr('data-kind') );
});
$("#field_type").val(fieldTypes.join(','));
Sign up to request clarification or add additional context in comments.

Comments

0

Maybe you've missed the plus sign? $("#field_type").val(field_type' '); should be $("#field_type").val(field_type+' ');

Comments

0

This code: http://jsfiddle.net/PKgkU/17/

Does what you want!

$('input').each(function(el) {
    switch ($(this).data('kind')) {
    case "video":
        kind = 'video';
        break;
    case "image":
        kind = 'image';
        break;
    case "title":
        kind = 'title';
        break;
    default:
        break;
    }
    $(this).after('<input type="hidden" id="field_type" name="field_type" value="' + kind + '" />');
});

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.