Ad

Ajax Response Not Working As Expected From PHP

- 1 answer

I am calling the JS function cancelAppointment on button click, I can confirm that the function is running okay as the first alert is shown.

I have followed many guides to try and get this to work but they are yielding no results.

At the moment the success message is not being showed and the row is not being deleted. I can confirm the PHP script works fine.

the function is as below:

<head>
<script>
    //Making the call to ajax this is encased in a function so it is not called pre-maturely
    function deleteAppointment()
    {
        jQuery(document).ready(function(){
        alert("Trying to run!");

            $.ajax({
                type : "POST",
                url : "http://www.website.com/delete_appointment.php",
                //data : "",                
                success : function(response) {
                    //Success
                    alert("Deleted");
                }
            }); 
        });
    }
</script>

For the moment, until I get the ajax to work the PHP contains PDO, that will delete from both the Appointments & AppointmentLines table with an ID of 1000. I will parse this in once this first part works.

Ad

Answer

In Wordpress the jQuery library included in the noConflict() mode, so the global $ shortcut for jQuery is not available, see more details here.

To make it work change $ to jQuery:

function deleteAppointment()
{
    jQuery.ajax({
        type : "POST",
        url : "http://www.website.com/delete_appointment.php",
        success : function(response) {
            alert("Deleted");
        }
    });
}
Ad
source: stackoverflow.com
Ad