1

I'm trying to make a form that is in a loop has a unique id. The issue with this is only the first form is working and all the rest after is not triggering the jquery code.

Form:

<?php foreach( $tasks as $tasks ) : ?>
<tr>
    <td><?php echo $tasks->title ?></td>
    <td><?php echo $newDate; ?></td>
    <td><?php echo $tasks->details ?></td>
    <td><?php echo $tasks->category ?></td>
    <td>

<form class="noclass">
    <input class="hideit id" type="text" name="id" value="<?php echo $tasks->id ?>" />
    <input type="checkbox" value="<?php echo $tasks->user ?>" name="value" id="checkbox-2-1" class="regular-checkbox big-checkbox" <?php echo $tasks->user ?> />
</form>

</td>
</tr>
<?php endforeach; ?>

Jquery:

<script>
    $(function() {
        $('#checkbox-2-1').click(function() {
            var id = $(".id").val();
            var value = $(".check").val();
            var dataString = '?id=' + id + '&value=' + value + '&user=<?php echo $id ?>';
            alert(dataString);

            $.ajax({
                type: "POST",
                url: "http://example.com/process.php",
                data: dataString,
                cache: false,
            });
        })
    })
    return false;
</script>
0

2 Answers 2

1

You are using

var id = $(".id").val();  
var value = $(".check").val();

and what $(".id") and `$(".check")' will return is object.

So you need to convert it into string of key value pair and then send it.

Also refer Gautam answer for foreach correction.

ONE MORE

You are repeating checkbox-2-1 id for checkbox evert-time loop gets execute and ID for each dom must be unique.

What i suggest

Use code as below.

for checkbox click event use

$('input:ckeckbox[name="value"]').on('change',function(){ });

and inside function(){ } write code for getting value of class id and check as below

$(this).closest('form.noclass').find('.id').val();
$(this).closest('form.noclass').find('.value').val()
Sign up to request clarification or add additional context in comments.

2 Comments

Sorry I'm a newbie, How can I alert what these values hold?
just create variable that hold value and then alert it as normal.. var id = $(this).closest('form.noclass').find('.id').val(); alert(id);
1

Your first form is not closing....close like this

<form class="noclass">
    <input class="hideit id" type="text" name="id" value="<?php echo $tasks->id ?>" />
</form>
<form class="noclass">
    <input class="hideit id" type="text" name="id" value="<?php echo $tasks->id ?>" />
    <input type="checkbox" value="<?php echo $tasks->user ?>" name="value" id="checkbox-2-1" class="regular-checkbox big-checkbox" <?php echo $tasks->user ?> />
</form>

And what is

<?php foreach( $tasks as $tasks ) : ?>

try to give another variable like

<?php foreach( $tasks as $task ) : ?>

and then change the symultanious

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.