PHP Contact Form

Hi All,
I have been struggling with running a php file for an html form I have on a website.
I built a similar php file for another website, and it works great, but for some reason this one is just not working. I get:
This page isn’t working

mywebsite.com is currently unable to handle this request.

HTTP ERROR 500

I know this is something simple, I’m just at a dead end right now.

Thanks

<?php
$nameFirst = $_POST['nameFirst'];
$nameLast = $_POST['nameLast'];
$email = $_POST['email'];
$website = $_POST['website'];
//$service = $_POST['service'];
$message = $_POST['message'];

$formcontent = "Sender name: $nameFirst $nameLast\n
Email: $email \n
Website: $website \n
Message: $message";

$recipient = "myemail@mywebsite.com";
$subject = "Web submission from $email";
$mailheader = "For My Company \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
?>

<html>
<head>
<title>Contact ZS | Thank You</title>
<link href="css_zs_master.css" rel="stylesheet" type="text/css">
<link rel="stylesheet" href="https://use.typekit.net/emu2xge.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
</head>
<body>
<div class="container">
<h5 class="form-data">Thank You <? php echo "$nameFirst"; ?>!</h5>
<h6 class="form-data">Your request has been sent and you will receive a follow up email shortly.</h6>
<a href="index-home.html"><button class="plain-button"><i class="fa fa-home"></i></i> Home</button></a>
</div>
</body>
</html>

Hello,

Did you check the error log?

Ghost love,

No I have not, how do I do that?

You need to locate the error_log file. Location may vary depending on your config.

You could try <?php phpinfo(); ?> and look for error_log.

This file contains all errors encountered when running your scripts.

Don’t you want to do some validation, though?
Also this script looks like it will attempt to send an email on page load.

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
  # Do some validation

  mail($recipient, $subject, $formcontent, $mailheader);
}

I admit, I’m a novice when it comes to validating forms.
Working on it but I need to read more on how to do the validation properly.

Right now the submissions are sent to my email, the goal is to have the data sent to an sql database. But that comes after I learn how to do that :slight_smile:

Does that mean it’s now working, or is that the expected result? :slight_smile:

I got the php log to work, and there was an invalid syntax on line 29 … <? php and not <?php

Submission works!!!

Now I need to work on the validation.

Thanks for pointing out the error log, that really helped, and I’ll use it a lot in the future.

Yes, it’s really helpful! Glad that you got it to work :slight_smile:

Feel free to come back here if you run into any more issues, always happy to help.

For sure, great support in this community. I like it a lot more than stackoverflow!!!

1 Like