PHP info not Working

Hi folks.
I’m trying to use PHP to send me an email when someone fills out a form.
I’m ok with HTML and CSS but PHP is an alien language to me.

What I’ve tried so far (With others helping me) is below. Unfortunately it doesn’t work.

When I fill out the form and hit submit I get the error page. “OOPS”

My html, which works fine, I think, is:

<div class="email">
<form action="/EmailForm/action_page.php">

<label for="fname">First Name</label>
<input type="text" id="name" name="name" placeholder="Your first name..">

<label for="lname">Last Name</label>
<input type="text" id="lname" name="lastname" placeholder="Your last name..">

<label for="emailadd">Email Address</label>
<input type="text" id="emailadd" name="emailadd" placeholder="Your email address..">

<label for="phone">Telephone</label>
<input type="text" id="phone" name="phone" placeholder="Your telephone number..">

<label for="message">Message</label>
<textarea id="message" name="message" placeholder="Write something.." style="height:200px"></textarea>
<input type="submit" value="Submit">
</form>
</div>

My PHP, which I’ve borrowed and tweeked, is:

<?php
$siteemailtosend="[[email protected]](mailto:[email protected])";
$siteemailtoreceive="[[email protected]](mailto:[email protected])";

if(empty($_POST['fname']) ||
empty($_POST['lname']) ||
empty($_POST['emailadd']) ||
empty($_POST['phone']) ||
empty($_POST['message']) ||
!filter_var($_POST['email'],FILTER_VALIDATE_EMAIL))
{
echo "Opps,!";
return false;
}
$name = strip_tags(htmlspecialchars($_POST['name']));
$email_address = strip_tags(htmlspecialchars($_POST['email']));
$phone = strip_tags(htmlspecialchars($_POST['phone']));
$message = strip_tags(htmlspecialchars($_POST['message']));
$to = $siteemailtoreceive;
$email_subject = "Website Contact From: $name";
$email_body = "You have received a new message from your website contact form.\n\r\n";
$email_body = $email_body."Here are the details:\n\r\n";
$email_body = $email_body."Name: $fname\n\r\n";
$email_body = $email_body."Last Name: $lname\n\r\n";
$email_body = $email_body."Email: $emailadd\n\r\n";
$email_body = $email_body."Phone: $phone\n\r\n";
$email_body = $email_body."Message: $message\n\n\r\n";
$headers = "From: $siteemailtosend\n";
$headers .= "Reply-To: $email_address";
mail($to,$email_subject,$email_body,$headers);
return true;
if ((mail($to,$subject,$message,$headers))&&(mail($autoTo, "Thank You from ", $autoreply, 'From: [[email protected]](mailto:[email protected])'))) {
header("Location: ".$goto_after_mail);
$success = "Your message has been sent. We shall reply as soon as we can";
}
?>

Please remember, my PHP knowledge can be written on a pin head.

The $_POST variable is an array containing the values that were submitted to the form, keyed by their names in the form. Your form does not have an input named "email", so the filter_var call is acting on the value null - this more or less means “no value”. This is obviously not a valid email, so you’re getting your error. You can fix it by changing the $_POST['email'] to whatever your email input is called - presumably $_POST['emailadd'] from looking at your form.

PHP can give you a little more help with this kind of thing if you enable error reporting. Use the following lines at the top of your script, just after the PHP opening tag:

ini_set('display_errors', 1);
error_reporting(E_ALL);

You should remove these once your site is live so that users don’t see any information about how your code runs.

That last post ain’t very reader friendly.
I only copy and pasted

You can use bbcode [code][/code] tags (on their own lines) or markdown three back-ticks ``` (on their own lines) before and after your code in order to format it.

I’ll give it a go and delete that post.
TA

OK time to retry.
For some reason I still can get these to work together.
If anyone can shed light I’d appreciate it.

HTML:

Contact Form

<label for="fname">First Name</label>
<input type="text" id="fname" name="firstname" placeholder="Your first name..">

<label for="lname">Last Name</label>
<input type="text" id="lname" name="lastname" placeholder="Your last name..">

<label for="email">Email Address</label>
<input type="text" id="email" name="emailadd" placeholder="Your email address..">
  
<label for="phone">Telephone Number</label>
<input type="text" id="phone" name="phonenumber" placeholder="Your telephone number.."> 
  
<label for="message">Message</label>
<textarea id="message" name="messagesent" placeholder="Write your message here.." style="height:200px"></textarea>

<input type="submit" value="Submit">

PHP:

<!doctype html>

<?php

$siteemailtosend=“Address deleted”; Deleted due to post rules for new users
$siteemailtoreceive=“Address deleted”; Deleted due to post rules for new users
if(empty($_POST[‘fname’]) ||
empty($_POST[‘lname’]) ||
empty($_POST[‘email’]) ||
empty($_POST[‘phone’]) ||
empty($_POST[‘message’]) ||
!filter_var($_POST[‘email’],FILTER_VALIDATE_EMAIL))
{

echo nl2br ("\n\n We're really sorry, but the form doesn't seem to be working. \n\n Please contact \n or call ");

return false;
}

$fname = strip_tags(htmlspecialchars($_POST[‘fname’]));
$lname = strip_tags(htmlspecialchars($_POST[‘lname’]));
$email = strip_tags(htmlspecialchars($_POST[‘email’]));
$phone = strip_tags(htmlspecialchars($_POST[‘phone’]));
$message = strip_tags(htmlspecialchars($_POST[‘message’]));
$to = $siteemailtoreceive;
$email_subject = “Website Contact From: $name”;
$email_body = “You have received a new message from your website contact form.\n\r\n”;
$email_body = $email_body.“Here are the details:\n\r\n”;
$email_body = $email_body.“Name: $fname\n\r\n”;
$email_body = $email_body.“Last Name: $lname\n\r\n”;
$email_body = $email_body.“Email: $email\n\r\n”;
$email_body = $email_body.“Phone: $phone\n\r\n”;
$email_body = $email_body.“Message: $message\n\n\r\n”;
$headers = “From: $siteemailtosend\n”;
$headers .= “Reply-To: $email_address\n”;
mail($to,$email_subject,$email_body,$headers);
return true;

if ((mail($to,$subject,$message,$headers))&&(mail($autoTo, "Thank You from ", $autoreply, ‘From: Address deleted’))) Deleted due to post rules for new users
{
header("Location: ".$goto_after_mail);
$success = “Your message has been sent. We shall reply as soon as we can”;
}
?>

For admin.
I won’t be posting for a few weeks.
Please keep this thread open.

Sponsor our Newsletter | Privacy Policy | Terms of Service