Executing PHP with Cloudflare Pages Functions

DISCLAIMER : I AM VERY NEW TO WEB DEVELOPMENT

I have started using Cloudflare pages with the idea in mind that they can host my website for free. Everything went exactly as I wanted (setting up custom domains, DNS, GitHub…) except my PHP.

For more context I have built an HTML Form which has an method of “POST” and an action of “my_php_file.php”. In my javascript file I validate the data with jQuery validation plugin and handle the submission with the jQuery Form plugin, links are provided below. My PHP file basically just has code for sending E-mails to Google’s SMTP server (smtp.gmail.com) by using the PHPMailer library, link is below. The important thing is that it worked perfectly well and I could send E-mails that have my HTML Form data flawlessly with the mentioned plugins and libraries using PHP’s built-in web server (link below).

However when I connected my GitHub repo with Cloudflare Pages and set it all up, the E-mail sending functionality stopped working. In DevTools under “Network” it listed my POST request with the status 405 “Method not allowed”.

After that I learned about Cloudflare Pages Functions which I’m guessing can provide the functionality I need, however I could not find the solution to my exact problem so here I am hoping that someone could explain to me thoroughly how do I achieve what I want. In my research I found some websites that are connected with my problem, but again I could not understand how to fix my problem with their help (links are below).

LINKS :

(https://jqueryvalidation.org/)
(https://jquery.malsup.com/form/)
(https://github.com/PHPMailer/PHPMailer)
(https://www.php.net/manual/en/features.commandline.webserver)

Some sites I found in my research:

(https://developers.cloudflare.com/pages/tutorials/forms/)
(https://developers.cloudflare.com/pages/platform/functions/api-reference/)

Pages is meant for static sites and doesn’t support PHP. Pages Functions enable you to execute JavaScript server side, but you’d have to rewrite the server side contact form in JavaScript.

If you need examples, consider checking this repository which includes contact form processing using Functions: GitHub - WalshyDev/pages-functions-contact: Create a contact form on Cloudflare Pages with Functions :slight_smile:.

4 Likes