Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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>
share|improve this question
 
see my updated answer.. –  Dipesh Parmar Jun 6 '13 at 5:29
add comment

2 Answers

up vote 1 down vote accepted

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()
share|improve this answer
 
Sorry I'm a newbie, How can I alert what these values hold? –  Frank Aguilar Jun 6 '13 at 5:36
 
just create variable that hold value and then alert it as normal.. var id = $(this).closest('form.noclass').find('.id').val(); alert(id); –  Dipesh Parmar Jun 6 '13 at 5:38
 
YES! It worked! thanks so much! –  Frank Aguilar Jun 6 '13 at 5:40
 
@FrankAguilar always welcome dude. –  Dipesh Parmar Jun 6 '13 at 5:42
add comment

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

share|improve this answer
 
See my edit also –  Gautam3164 Jun 6 '13 at 5:17
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.