phpformsnetlify

How to run php files on 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 = "sample@email.com";
$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.


Solution

  • 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.