I’ve created a custom template for wordpress and I’m trying to execute some php code (deleteUser.php) to delete a record from a db but I’m not sure how to run the code without using alert. At the moment I’m just getting an empty dialog box rather than my php code deleting a row from the db.

ajax part

<?php
    echo '<script>';
    echo    'function dltUser(siteID){';
    echo    "jQuery.ajax({";
    echo        "data: 'siteID=' + siteID,";
    echo        "url:"."'".get_stylesheet_directory_uri()."/deleteUser.php"."',";
    echo        "method: 'POST', ";
    echo        "success: function(msg) {";
    echo            "alert(msg);";
    echo            "}";
    echo        "});";
    echo    '}';
    echo '</script>';
?>

deleteUser.php – the code to delete a row

<?php
include_once($_SERVER['DOCUMENT_ROOT'].'/wp-config.php' );
global $wpdb;
$siteID = $_POST['siteID'];
$wpdb->query("DELETE FROM my_site WHERE SiteID = %d",$siteID);

?>

Read more here: Using AJAX to execute a php file


Solution:

If you know the solution of this issue, please leave us a reply in Comment section, to update the question.


Wordpress related questions and answers: