j.m.l
j.m.l

Reputation: 27

Why is my onclick function not called?

?php foreach($participants as $participant) {
        echo "<tr>";
            echo "<td>" . $participant['Vorname'] . "</td>";
            echo "<td>" . $participant['Auto'] . "<img id=$participant[name_id] onclick='deleteDriver()' class='delete' src='img/tonne.gif' align='right' data-url='backend/queries.php?decision=deleteDriver&id=".$participant['name_id']."'/></td>";

        echo "</tr>";
    }
    ?>

        function deleteDriver()
        {
            var dataString = "decision=deleteDriver";
            $.ajax({
                type: 'POST',
                url: 'queries.php',
                data: dataString
                }
            })
        }
    </script>

When I push the button the function is not called. Why?

Upvotes: 1

Views: 65

Answers (2)

AnkiiG
AnkiiG

Reputation: 3488

You have missed <script> open tag before function deleteDriver(). Please add and retry. Try as below :

<?php
foreach($participants as $participant) {

  echo "<tr>";
    echo "<td>" . $participant['Vorname'] . "</td>";
    echo "<td>" . $participant['Auto'] . "<img id='".$participant['name_id']."' onclick='deleteDriver()' class='delete' src='img/tonne.gif' align='right' data-url='backend/queries.php?decision=deleteDriver&id=".$participant['name_id']."'/></td>";
  echo "</tr>";
}
?>
<script>
        function deleteDriver()
        {
            var dataString = "decision=deleteDriver";
            $.ajax({
                type: 'POST',
                url: 'queries.php',
                data: dataString
                }
            });
        }
</script>

Upvotes: 1

Priya Rajaram
Priya Rajaram

Reputation: 360

try with this code.

<?php
    foreach($participants as $participant) {
            echo "<tr>";
                echo "<td>" . $participant['Vorname'] . "</td>";
                echo "<td><a onclick='deleteDriver()'>" . $participant['Auto'] . "<img id=$participant[name_id] class='delete' src='img/tonne.gif' align='right' data-url='backend/queries.php?decision=deleteDriver&id=".$participant['name_id']."'/></a></td>";
           echo "</tr>";
        }
        ?>
      <script>
            function deleteDriver()
            {
                var dataString = "decision=deleteDriver";
                $.ajax({
                    type: 'POST',
                    url: 'queries.php',
                    data: dataString

                })
            }
        </script>

Upvotes: 1

Related Questions