0

I seem to be having an issue deleting rows from my database using a function I created called "DeleteVendor". I can get the Confirm box to pop up but the moment I add the php portion it stops.

<script text='language/javascript">

function DeleteVendor(vendor_id, vendor_name)
{
  if (confirm("Are you sure you want to delete " + vendor_name))
  {
    // perform delete operation
    <?php
      $conn = new mysqli($servername, $username, $password, $dbname);
      // Check connection
      if ($conn->connect_error) {
      die("Connection failed: " . $conn->connect_error);
      }
      $sql = "SELECT * FROM vendors order by vendor_id";
      $result = $conn->query($sql);
      if ($result->num_rows > 0) {
        $sql = "DELETE FROM vendors WHERE vendor_id = 'Delete'";
        
      }
    $conn->close();
    ?>
  }
}
</script>

Buttons:

<td style='text-align:center'>
<input style='border-color:#0000FF;background-color:#0000FF;color:#FFFFFF;height:25px;margin-right:15px' type='submit' onclick='EditVendor(" . $row["vendor_id"] . ")' value='Edit' />
<input style='border-color:FF0000;background-color:#FF0000;color:#FFFFFF;height:25px' type='button' onclick=\"DeleteVendor(" . $row["vendor_id"] . ", '" . $row["vendor_name"] . "')\" value='Delete' />
</td>

I've searched this site along with w3schools trying a few different methods mentioned but have had no luck. I've seen examples that use a "hidden" button in addition to the onClick button, when I tried this nothing happened still. Any help would be appreciated thank you.

Krazmad
  • 35
  • 4
  • Please see https://stackoverflow.com/questions/13840429/what-is-the-difference-between-client-side-and-server-side-programming. The PHP code will run on the server before the page is rendered, not when you click the button. – Don't Panic Jun 14 '22 at 20:23
  • place your input inside a form. submit your form and go from there – DCR Jun 14 '22 at 20:44
  • You are looking for Ajax to perform this kind of actions – Lelio Faieta Jun 14 '22 at 20:57

0 Answers0