19

I'm trying to test out if PHP works from my Firebase hosting using the following:

(index.html)

<form action="welcome.php" method="post">
   <input type="submit">
</form>

(welcome.php)

<?php

   $to = "my@email.com";
   $subject = "My subject";
   $txt = "Hello world!";
   $headers = "From: dummy@email.com";

   mail($to,$subject,$txt,$headers);

?>

Every time I try this the browser keeps on attempting to open the PHP file rather than processing it. Is simple PHP enabled on the Firebase server hosting to process a simple form like this? If I can get it to work this way, I will be building the form out correctly including validation etc.

Thanks,

Richard Ansell
  • 916
  • 1
  • 12
  • 28

4 Answers4

44

From the Firebase Hosting site (emphasis mine):

We deliver all of your static content (html, js, images, etc.) over a secure SSL connection and serve it on a CDN.

Firebase Hosting is for hosting static assets. Firebase currently doesn't offer any way to execute your code on Firebase's servers.

Update (2018-08-08): You can now run Node.js/JavaScript code but connecting your Firebase Hosting project to Cloud Functions + Firebase Hosting. But that still won't allow you to run PHP code.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Thanks for clarifying this furthermore, I didn't know whether this would affect PHP processing, which is obviously the case now. Is there any other way to process a simple form to email form contents using Firebase hosting? – Richard Ansell Feb 20 '16 at 13:54
  • I've had a look at this already, although I'd rather the website process the form contents directly and email the person. I have read that you can integrate Zapier as another option with Firebase, although I'm not sure if this is what I need (https://zapier.com/zapbook/firebase/mandrill/). – Richard Ansell Feb 20 '16 at 13:59
  • Firebase Functions are just a easy-way to build a G Cloud Function. In GCF you can run many different languages, including PHP – Guilherme Ferreira Mar 09 '21 at 01:20
3

As per the latest update firebase has started using Cloud Functions

Cloud Functions for Firebase lets you run mobile backend code that automatically responds to events triggered by Firebase features and HTTPS requests. Your code is stored in Google’s cloud and runs in a managed environment. There's no need to manage and scale your own servers.

For more : https://firebase.google.com/docs/functions/

3

There is no PHP but nodeJS available for server-side scripting ...

Google Cloud Functions are written in JavaScript, and execute in a Node.js runtime.

Mandrill also supports nodeJS and it features a Webhooks API. Therefore, one can require that node module within these "cloud functions" and "web hooks" ...and then post with a HTML form onto them.

There would need to be a few HTTP cloud functions defined on the Firebase Console, in order to let them subscribe, unsubscribe and manage their subscriptions. One could even generate the HTML markup for the input form with cloud functions and then attach it. As an example, not tested and no guarantee included:

const functions = require('firebase-functions');
const mandrill = require('mandrill-api/mandrill');
var client = new mandrill.Mandrill('YOUR_API_KEY');

/* TODO: add the user on Firebase, respond through the API */
exports.user_add = functions.https.onRequest((req, res) => {

});

/* TODO: change subscription settings on Firebase, respond through the API */
exports.user_edit = functions.https.onRequest((req, res) => {

});

/* TODO: remove the user on Firebase, respond through the API */
exports.user_remove = functions.https.onRequest((req, res) => {

});

/* optional: generate the HTML markup of the form, send HTTP response */
exports.markup = functions.https.onRequest((req, res) => {

});

One can bind the events of Firebase Auth, to keep two user databases in in-sync (this is not required for Mandrill, but required for MailChimp - no matter whether using the PHP or nodeJS wrapper):

exports.on_user_create = functions.auth.user().onCreate(event => {
   const user = event.data;
});

exports.on_user_delete = functions.auth.user().onDelete(event => {
   const user = event.data;
});

Firebase on Websites explains it, while there is a Local Emulator for Cloud Functions.

Martin Zeitler
  • 1
  • 19
  • 155
  • 216
1

You can play around with any of these: Angular, Ember, Knockout, React, Node JS. The same thing you PHP code does you can make happen with pretty much any Javascript technologies - just no dynamic language. Also another way to do it is to used an online form providers like Jot Forms or others. You can create and style the form withing you online form account then simply add it to you site. Then when user post it will post to the form. As a result you have a centralized environment not only for you current site but for any others down the road. You can create a web service and post values there - then do whatever you want with them: save them to the database... Otherwords have another server that handles all those things so you can just call it from Firebase hosted sites. Hope that helps

PS: I am currently building a product that is a simplified version of Online Forms to be used on Firebase websites. I am planning to have a few people using for now so if you would like you can email me and I will create an account for you to use it. As long as there is no abuse like sending a bunch of emails - you will be fine!

enter image description here

Alexey Shevelyov
  • 926
  • 1
  • 13
  • 24