Jump to content

Using custom PHP code inside jQuery Ajax function


dps123
 Share

Recommended Posts

Hi

I'm having a torrid time with some Ajax code and a custom PHP script. I'll try to explain what I'm trying to do.

I have a form with a single email field. I validate the email field and if it's okay, I then make an Ajax call to my custom PHP script which needs to do some ProcessWire stuff to the user with that email address.

What I'm finding is, if I put my PHP code inside the templates folder, it is not executing at all. From what I've read, ProcessWire denies access to PHP files in this directory. So I've then tried putting the PHP file in the root of my site, outside of PW. It then can see and execute the PHP file. The issue then is that i can't access the PW commands to make the changes to the user. At least, I don't think I can from outside of PW.

Any help gratefully received!

Link to comment
Share on other sites

There are some points you're missing.

ProcessWire does not (really) deny access to php files in the templates folder, it just denies direct access to those php files, because they would most likely not work correctly. You can however use any of those php files in that folder if you have a template using it as well as a page using the template. Just use the page's url in your js file and the linked php file will be called. Only by accessing a page instead of the file directly ProcessWire is bootstrapped before the php (template) file is executed and all your api variables are available.

But you could also use a custom php file, without the need to have a template and page set up. You can also manually have ProcessWire bootstrapped by including it like it's described here: https://processwire.com/api/include/

  • Like 2
Link to comment
Share on other sites

Thanks for the help LostKobrakai. I still couldn't get it to work with the file inside templates, even though I had a page in PW using that template. If I changed the path in the JS to that page's path, it didn't work. It does work with the PHP page in the root folder though.

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

×
×
  • Create New...