This topic is locked
[SOLVED]

Verify if child records exist before deleting

4/3/2024 3:51:17 PM
PHPRunner General questions
A
asawyer13 authorDevClub member

I have a custom button on my edit page.

I want to verify that no records are using the record id of what the user is trying to delete.

It almost works. Here's my two issues. On the Server side of the button I have this code

global $dal;
$tbl = $dal->Table("Files");
$rs = $tbl->Query("Gender=".$_SESSION["ID"],"");
$data = db_fetch_array($rs);
if($data)
{
echo '<script>alert("Can not delete as this is being used")</script>';
}
else
{
$data = array();
$data["GenderID"] = $_SESSION["ID"];
DB::Delete("Genders", $data );
}
  1. So if there are records that use it I do get the alert, but when I click on OK, I get a server error, though I can't quite figure out what the error is.
  2. If there are not child records, it deletes the record as it should, however I'd like to either how a message saying it was deleted, and then return to the List page, or just return to the list page. Obviously I would want the list page to refresh.

Thanks for any help,
Alan

Sergey Kornilov admin 4/4/2024

You cannot output anything in button's Server event. Buttons are executed via AJAX and return JSON to ClientAfter event. Any output you add will break the JSON.

If you need to display a message, you need to do this in Javascript event like ClientAfter.