Zain
Zain

Reputation: 1252

Ajax PHP script not running

I want a PHP script to run on form submit and use Ajax to stop the page refreshing.

The JavaScript code successfully reaches the .done method and I put a console.log into the first line of my php script which doesn't seem to be getting triggered.

JS

$('.enquire-form').submit(function(e) {
  e.preventDefault();
  $this = $(this);
  $.ajax({
    type: "POST",
    url: "post.php",
    data: $this.serialize()
  }).done(function(data) {
    alert("done");
  }).fail(function( jqXHR, textStatus ) {
    console.log("Request failed: " + textStatus);
    alert( "Request failed: " + textStatus );
  });
});

PHP (originally it was console.log but have changed based on suggestions)

echo "hello world";

Folder directory (enquiry.html has the form in it) enter image description here

Thanks :)

Upvotes: 0

Views: 103

Answers (1)

Chase
Chase

Reputation: 3105

Unless something has changed, you can't execute JS that has been returned from an AJAX call. See this answer.

Make sure you are attempting to access the returned data via the data var inside your done function.

Upvotes: 1

Related Questions