How to run php files on netlify? - php

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.

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.

I recommend using InfinityFree for hosting the PHP site for free. or if your need is just to send an email form and the rest is static pages then i recommend emailjs that will let you send emails for free. so that you can deploy your site in netlify or vercel

You can use 000webhost.com hosting for uploading your php projects.

Related

PHP Email Form Issue: Cannot POST

I've been working on getting a PHP email form set up on my personal webpage, and I am running into an issue where when I hit submit, the page displays "Cannot post /mail.php".
I've seen several similar questions on this site before, but I haven't found any approaches that have solved the issue. I am new to PHP so I'm not sure how deep the error could be, or if I'm possibly missing something obvious.
I'm using Brackets Live Preview on Mac OS Big Sur.
Things I've tried:
Made sure that the mail.php file is in the same directory as my HTML files
Made sure I'm setting the name attribute in my HTML file so the PHP can pick up on the relevant entered information
Switched POST with GET (POST errors, GET download the PHP file instead of running it)
Downloaded PHP with Homebrew
Redownloaded Brackets
For reference this is my code:
HTML File:
<form action="mail.php" method="post">
<p>Name</p> <input type="text" name="name">
<p>Email</p> <input type="text" name="email">
<p>Message</p><textarea name="message" rows="6" cols="40"></textarea><br>
<input type="submit" value="Send"><input type="reset" value="Clear">
</form>
PHP File:
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$formcontent="From $name \n Message: $message";
$recipient = "email.redacted#gmail.com";
$subject = "Contact Form";
$mailheader = "From $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error");
?>
Any insight into what may be going wrong or some debugging steps to take would be much appreciated!
Here <? php you have whitespace after the ?. Remove it, and see what happens.
It should be <?php to be valid syntax. Otherwise the script breaks.
Edit: I'm not claiming this will solve any logical errors in your code, but that space in there is making the script not even PHP.
Is your form you have method="get". For you to access the information using $_POST you'll need to change it to method="post".
<form action="/mail.php" method="post">
<p>Name</p> <input type="text" name="name">
<p>Email</p> <input type="text" name="email">
<p>Message</p><textarea name="message" rows="6" cols="40"></textarea><br>
<input type="submit" value="Send"><input type="reset" value="Clear">
</form>
With regards to why your Bracket Live Preview is downloading the PHP file instead of running it. It sounds like your Bracket Live Preview isn't configured correctly.
You might be able to get some help from this question.
Using Brackets for PHP files

My Simple Contact.php form is not working

I am working on one website and wanted to add a contact form in it but when I tried to create simple contact for testing with fields that I am going to add on the website later it's not working properly.
I have tried some youtube videos for setup my contact.php form I followed every step but when I test it directly shows Error 500.
Specific Error is
This page isn’t working
www.creativeboxx.co.in is currently unable to handle this request.
HTTP ERROR 500
Here is my Testing HTML Form
<form action="contact-form.php" method="post">
<input name="name" placeholder="Full Name">
<input name="mail" placeholder="Email Address">
<input name="mobile" placeholder="Mobile Number">
<input name="reason" placeholder="Reason for Contact">
<textarea name="message" placeholder="message"></textarea>
<button name="submit" type="submit">Send</button>
</form>
I have tried with a few different PHP codes using youtube but nothing working
Here is my last used PHP code for contact.php
<?PHP
if (isset($_POST['submit'])){
$name = $_POST ['name'];
$mailFrom = $_POST ['mail'];
$mobile = $_POST ['mobile'];
$reason = $_POST ['reason'];
$message = $_POST ['message'];
$mailTo = "vishal.pandya#openxcell.info";
$headers = "From: ".$mailFrom;
$txt = "New Inquiry from " .$name.".\n\n".$message;
mail($mailTo, $reason, $txt);
header("Location: index.html");
}
?>
I have learned HTML and CSS only a couple of months ago so I don't have much knowledge about PHP so guys please help me out.
Thanks in Advance.
There could be many reasons for the error, what I can see :
please check if the PHP file name is: contact-form.php and it is in the same directory with your HTML code file (index.html or whatever)
mail($mailTo, $reason, $txt); => this function mail() is inbuild function in PHP.
to use mail() function, you will have to edit php.ini. and this function sometime creates issues and sometime not able to send email.
to send an email please use this library : phpmailer . this is a more widely used and battle-tested library, so it may help you to solve the issue
First, you need to know what error is happening on your page.
This way you can display errors in your page:
error_reporting(E_ALL);
ini_set('display_errors', 1);
when you hit the submit form you need to stop the redirect in order to view the error:
header("Location: index.html"); you need to disable this line to view the error
and most importantly when/if you solve the problem don't forget to remove the display error message (above code) because that would cause problems later on.

Azure PHP Contact Form to email issues

So I have a contact form and a PHP script coded on Dreamweaver. All has been uploaded to the Remote Server. A Web App has been created in Azure.
However, upon testing this, I get the good old "This page cannot be displayed" page that we all love!
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>
<body>
<?php
if(isasset($_POST{'formSubmit'})){
$name=$_POST['firstName'];
$surname=$_POST['surname'];
$companyName=$_POST['companyName'];
$email=$_POST['yourEmail'];
$to='todd.gilbey#indomtrading.com';
$subject='Form Submission';
$message="Name: ".$name."\n"."Surname: ".$surname."\n". "Wrote the following: "."\n\n".$companyName;
$headers="From: ".$email;
if(mail($to, $subject, $message)){
echo("Sent Successfully");
}
}
?>
</body>
</html>
<form action="handler_mail.php" method="post">
<input class="formInput" type="text" name="firstName" placeholder="First Name"/><br>
<input class="formInput" type="text" name="surname" placeholder="Surnames"/><br>
<input class="formInput" type="text" name="companyName" placeholder="Company/Production Name"/><br>
<input class="formInput" type="email" name="yourEmail" placeholder="Email address"/><br>
</form>
According to the Azure help team, the web app person already has PHP enabled so I don't see what the problem is.
I have my coding here to see if anyone has any idea what I'm missing.
Thanks
You have a typo in your code:
if(isasset($_POST{'formSubmit'})){
should be
if(isset($_POST{'formSubmit'})){
that might cause an error.
In Azure App Service, you should put all website pages into the default folder for IIS (D:\home\site\wwwroot).
You can change it from the Azure portal if needed.

Sending email through PHP?

I havent used any PHP, and wanted to make a form on my html file send an email without using mailto:
I have looked online for some tutorials on how to do this, and the best I could do myself was this.
<form method="POST" action="contact.php">
<input type="text" name="FirstName" placeholder="First Name" size="24px"/> <input type="text" name="LastName" placeholder="Last Name" size="24px"/><br>
<input type="tel" name="Phone" placeholder="Address" size="53px"/><br>
<input type="text" name="Address" placeholder="Phone Number" size="53px"/><br>
<input type="email" name="email" placeholder="Email Address" size="53px"/><br>
<input type="email" name="confirmemail" placeholder="Confirm Email" size="53px"/><br>
<input type="submit" name="submitButton" />
</form>
and for my PHP file the best I could try to get was this:
<?php
if($_POST["submit"]) {
$recipient="basketgiftsreceiver#gmail.com";
$subject="Form to email message";
$sender=$_POST["Email sender"];
$senderEmail=$_POST["basketgiftssender#gmail.com"];
$message=$_POST["This is my message"];
$mailBody="Name: $sender\nEmail: $senderEmail\n\n$message";
mail($recipient, $subject, $mailBody, "From: $sender <$senderEmail>");
$thankYou="<p>Thank you! Your message has been sent.</p>";
}
?>
When I click my submit button, all I get is this:
If any of you guys could help me with this, that help would be much appreciated.
Thanks
By the looks of your issue you are not running a web server on localhost.
https://www.apachefriends.org/index.html
Php is server side scripting and does not run in the web browser.
Scripts are only executed when you access them through a webserver. You can't use a local file to run a script. Accessing the script using file://path will just display the contents of the script, not run it.
Beyond the fact that you probably not running a web server that supports php,
You are probably missing a smtp server, you need that to send an email in the code.
Google provides Information about their smtp.
server address : smtp.gmail.com
ports :
465 (SSL required) or 587 (TLS required)

Redirecting from html to php

I am trying to write a website from scratch and try to add a form.
but if i click on the Submit button (which redirects to the contact.php) it should send a mail without opening any email program, but it wont redirect properly... I googled and didnt find anything...
the code of that form is:
<!--Some code over this code-->
<form action="contact.php" method="post">
<font color="ffffff">
<center>
<div>
<label for="name">Name:</label>
<input type="text" id="name" />
</div>
<div>
<label for="Username">Username(s):</label>
<input type="text" id="name" />
</div>
<div>
<label for="Job">Job:</label>
<select name="Job">
<option value="Artist">Artist</option>
<option value="Musican">Musican</option>
<option value="Props">Props</option>
<option value="Voice">Voice</option>
<option value="Effects">Effects</option>
</select>
</div>
<div>
<div>
<label>THE THING</label><br><br>
<input type="checkbox" name="Program" value="Skype">I have SKYPE<br>
<input type="checkbox" name="Program" value="Steam">I have STEAM <br>
<input type="checkbox" name="Program" value="Origin">I have ORIGIN <br>
<input type="checkbox" name="Program" value="Teamspeak">I have TEAMSPEAK <br>
</div>
</div>
<div>
<label for="Message">Message:</label>
<textarea id="Message"></textarea>
</div>
<br>
<div class="button">
<button type="submit">Send your message</button> <button type="reset">Clear the fields</button>
</div>
</center>
</font>
</form>
<!-- More code from my html -->
but when i now click on that button which should redirect from the index.html to contact.php it just gives an error (I tried Google drive and Github to host and on Dropbox it just wants to download the contact.php...) Gdrive error: 404 Page not found, Github: 405 no permission and like i said, dropbox just wants to download the file...
The code of the PHP:
<?php
$ToEmail = 'mymail#domain.com';
$EmailSubject = 'NEW CONTACT! READ IT STOOPID';
$mailheader = "From: ".$_POST["name"]."\r\n";
$mailheader .= "Reply-To: ".$_POST["name"]."\r\n";
$mailheader .= "Content-type: text/html; charset=iso-8859-1\r\n";
$MESSAGE_BODY = "Name: ".$_POST["name"]."";
$MESSAGE_BODY .= "Username: ".$_POST["Username"]."";
$MESSAGE_BODY .= "Job: ".$_POST["Job"]."";
$MESSAGE_BODY .= "Program".$POST["Program"]."";
$MESSAGE_BODY .= "Message: ".nl2br($_POST["Message"])."";
mail($ToEmail, $EmailSubject, $MESSAGE_BODY, $mailheader) or die ("Failure");
?>
<center><h1>Thank you :D!</h1></center>
Is it because i use such cheap websites to host? I just dont want to spent thousands of bucks into a little site i created for planning, as well as learning HTML...
Welcome to StackOverflow.
Your error message says "gdrive and github cannot find the file". That is because, although they have your file, they are not set up to execute your files as PHP. Your issue here is that you are trying to run a php script in a non-server environment. You can set up a server environment on your own PC using XAMPP or WAMP Server. If you're using a Mac, you can use MAMP (free version or Pro).
Once you have installed the server bundle, you will have the most common Apache/PHP/MySQL setup. You will need to install your code in the X:/xampp/htdocs 1 folder (by default). To run your scripts, open your browser and go to http://localhost.
Also, domain name and hosting can cost as little as £16 per year for a .co.uk domain. .coms cost more. Other Top-Level Domains (TLDs) are available.
'X' is the drive letter for the drive in which you installed XAMPP or similar. For WAMP, use X:/wamp/www.
I think your «websites to host» does not support PHP. You need web server like «apache» for supporting PHP scripts. You can test this by creating a file called 'test.php' (or give it another name, just make sure it has .php as extention) and put in the following code:
<?php phpinfo() ?>
If your host supports PHP, you should get a confirmation while opening test.php with the PHP version they are running. If you do not see this confirmation, your host does not support PHP.

Categories