0

I have a php foreach statement which works. I have a jquery button on one of the echos. Below that the associated jquery function. On page load only the first button fires and the rest below don't. can anyone help?

//include 'database.php';
               $pdo2 = Database::connect();
               $sql2 = 'SELECT * FROM animals WHERE riderid = '.$data[id].'';
               foreach ($pdo2->query($sql2) as $row) {
                        echo '<tr>';
                        echo '<td>'. $row['hp'] . '</td>';
                        echo '<td>'. $row['hpname'] . '</td>';
                        echo '<td>'. $row['hpage'] . '</td>';
                        echo '<td>'. $row['hpcolour'] . '</td>';
                        echo '<td>'. $row['hpmicro'] . '</td>';
                        echo '<td>';
                        //echo '<a class="btn" href="read.php?id='.$row['id'].'">More Info</a>';
                        //echo ' ';
                        echo '<a class="btn btn-success" href="update.php?id='.$row['id'].'">Update</a>';
                        echo ' ';
                        echo '<button class="btn btn-danger" id="complexConfirm">Delete Record</button>';
                        echo '</td>';
                        echo '</tr>';
               }
               Database::disconnect();
              ?>

                </div>

                </tbody>
                </table>

            <script>

            $("#complexConfirm").confirm({
            title:"Are You Sure You Want To Delete this <?php echo $data['hp1'];?> called <?php echo $data['hpname1'];?>?",
            text: "Please Note that this Record will be archived just in Case.",
            confirm: function(button) {
                button.fadeOut(2000).fadeIn(2000);

                $.ajax({
                url: 'deletehorse.php?id=<?php echo $id;?>&hp=hp1&hpname=hpname1&hpage=hpage1&hpcolour=hpcolour1&hpmicro=hpmicro1',
                success: function(){
                //alert('done');
                window.location.reload(true);
            }
            });


            },
            cancel: function(button) {
                button.fadeOut(2000).fadeIn(2000);

            },
            confirmButton: "Yes I am",
            cancelButton: "No"
            });

        </script>
4
  • You have n-number of id="complexConfirm" buttons, which is invalid html. So jQuery will only select the first. Change to a class / class selector. Commented Mar 24, 2014 at 23:23
  • 1
    Unrelated but it looks like you're running a second query based on the results of a first, you may want to consider using a join in your query instead Commented Mar 24, 2014 at 23:25
  • Also, since your <script> is outside your loop, $data['hp1']; will only be the last selection, not based on the row you want. Commented Mar 24, 2014 at 23:25
  • Can I add the script inside the loop? If so can anyone show me how? Commented Mar 24, 2014 at 23:31

1 Answer 1

1

The buttons all have the same ID, which won't bind properly using javascript. Try changing the ID to a class, and binding to the class instead.

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

2 Comments

Can I add the jquery <script> inside the loop? If so can anyone show me how? the reason for this is i need the script to be relative to pass url id's for a deletion php script.
@user3052324 I would keep the javascript outside of the loop, and instead of filling in the information with PHP, use javascript to pull the information from the correct column of the row of the table that was clicked. Write your javascript from the perspective of a fully generated HTML page.

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.