Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run php files on netlify?

Tags:

forms

php

netlify

I build a website which has a form. Contact form redirects to contact.php on submit. For some reason whenever I submit, it says page not found.

index.html

...
<form action="contact.php" method="post" enctype="text/plain">
  Name:<br>
  <input type="text" name="name" class="form-control" required><br>

  E-mail:<br>
  <input type="email" name="mail" class="form-control" required><br>

  Message:<br>
  <input type="text" name="comment" size="50"  class="form-control" required><br><br>

  <button type="submit" value="Send"> Send Message </button>
 </form>
 ...

contact.php

<?php

if($isset($_POST['submit']))
{
$name = $_POST['name'];
$mailFrom = $_POST['mail'];
$message = $_POST['comment'];

$mailTo = "[email protected]";
$headers =  "From: ".$mailFrom;

mail($mailTo, $name, $message, $headers);

header("Location: index.html");
}
?>

I added a build.sh file containing:

#!/bin/bash
php contact.php

I also added ./build.sh in build command. I feel my script is wrong. Please suggest me alternatives to solve this problem.

like image 407
Sohel Shaikh Avatar asked Aug 27 '18 15:08

Sohel Shaikh


People also ask

Can you run PHP files on Netlify?

Buddy CI/CD allows you to instantly integrate PHP with Netlify to automate your development and build better apps faster.

Does Netlify support PHP and MySQL?

Netlify is an awesome service that lets you do continuous integration using git. Each time you push commits, the service gets the changes and rebuild your website. The main issue is that it does not support PHP files, that's why i decided to work on a process to convert PHP website into a static website.


1 Answers

A Netlify site is deployed to a CDN and serves up static content and although you can run PHP at the time of a deploy, you cannot execute PHP during a page request.

To submit a form, you can use Netlify Forms or some other serverless forms solution.

like image 113
talves Avatar answered Sep 22 '22 02:09

talves