Jump to content

Ajax help please - Correct way to implement an external php script


Greg Lumley
 Share

Recommended Posts

Hi, I've read up quite a bit about connecting to Ajax but it's going over my head as to how to implement it properly.  I can get it to work when I put the processing script in the root but that's obviously not the correct way to do it. 

The page in question is a simple contact form that calls a php script using axios framework to process and send the email. 

This is the basic js code I have where ajax_contact.php is the processor... (linked to a form within a template.)
I have read about $config->ajax etc... but still don't get how I should be implementing the "processor php script" correctly. 

Not that it's needed but this is the form I'm working on: https://www.lumleystudios.com/#contact

  axios.post('ajax_contact.php', params)
  .then(function (response) {
    console.log(response);
    //document.getElementById('output').innerHTML = response.data;
  })

Pardon me if this sounds like a dim question, I have an odd way of not seeing it the way everyone else does at time.s

Thank you! 

Greg 

 

Link to comment
Share on other sites

Hello @Greg Lumley,

Quote

but still don't get how I should be implementing the "processor php script" correctly. 

From what I understand (and I am not an expert as are the others here) there are two ways to process an ajax request: 1, create a page with corresponding template and php file (preferred/common method), or 2, create a php file outside of the site folder. Which method you choose has different requirements.

The common method is to create a php file (in templates folder) named ajax_contact.php to handle the ajax request, assign that file to a template named ajax_contact, and create a page for that template named ajax_contact. Your ajax url them becomes the name of that page, ie, /ajax_contact/. Note that the page should be published or PW will ignore it during your request.

Method two I think requires that you initially load PW (index.php) in your ajax_contact.php file since you are outside the document root referenced during the processwire installation. In this case, your ajax url would be the actual php file name; ajax_contact.php.

I don't know which method is preferred, but I use the first method simply because that is the way processwire handles normal page request. I think you have both methods implemented in your script at the same time when it should be one or the other.

I hope this helps. I'm sure someone more knowledgeable will provide you with a better answer, and hopefully correct me as well.

 

  • Like 3
Link to comment
Share on other sites

u.can do on  anyee page template like .this

if($config->ajax){
  // u do.ajax
  $data=['status'=>'ok', 'massage'=>'hi whirld'];
  header('content-type: application/json');
  echo json_encode($data);
  exit;
}else{
  // u.do html
}

  • Like 6
Link to comment
Share on other sites

Couple of other notes:

Quote: "All $config->ajax does is checking for the request header "X-Requested-With", which is not even a real standard. It is included in jquery's ajax tools by default, but most modern ajax libraries don't do that anymore. There's nothing more to it."

Quotes: "Make sure your post url ends with a slash." "Otherwise the .htaccess (or php not sure) will do a redirect, which essentially strips all the headers sent with the request."

Also, use $this->halt() instead of exit() or die(), read more on it here. (such as: note that return $this->halt(); must be called from directly within the template file, outside of any function or class scope)

Use Tracy Debugger to ease the pain ? https://processwire.com/talk/topic/12208-tracy-debugger/page/31/?tab=comments#comment-135558

 

  • Like 2
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...