Using AJAX to execute a php file

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

    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

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

Leave a Reply

Your email address will not be published. Required fields are marked *