I'm having a weird error that I cannot figure out with a PHP file, I've never touched PHP and I am trying to use it to send a Name, Email Address, and a message back to me from a website, but for some reason, the code I wrote is just downloading the PHP file instead, can anyone else see where I may be going wrong here:
HTML
<div class="modal-body">
<div class="container"> <!-- TODO add in contact form -->
<form action="form-to-action.php">
<label for="fname">Name</label>
<input type="text" id="Name" name="Name" placeholder="Name">
<label for="lname">Email Address</label>
<input type="text" id="Email" name="Email" placeholder="Email Address">
<br>
<label for="subject">How can we Help?</label>
<textarea id="Message" name="Message" placeholder="Write something.." style="height:200px"></textarea>
<input type="submit" value="Send"id="contactBtnModal"style="text-decoration: none;" >
</form>
</div>
</div>
PHP:
<?php
if (isset($_POST['Email'])) {
// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "testingemail#gmail.com";
$email_subject = "Testing";
function problem($error)
{
echo "We are very sorry, but there were error(s) found with the form you submitted. ";
echo "These errors appear below.<br><br>";
echo $error . "<br><br>";
echo "Please go back and fix these errors.<br><br>";
die();
}
// validation expected data exists
if (
!isset($_POST['Name']) ||
!isset($_POST['Email']) ||
!isset($_POST['Message'])
) {
problem('We are sorry, but there appears to be a problem with the form you submitted.');
}
$name = $_POST['Name']; // required
$email = $_POST['Email']; // required
$message = $_POST['Message']; // required
$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-]+#[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
if (!preg_match($email_exp, $email)) {
$error_message .= 'The Email address you entered does not appear to be valid.<br>';
}
$string_exp = "/^[A-Za-z .'-]+$/";
if (!preg_match($string_exp, $name)) {
$error_message .= 'The Name you entered does not appear to be valid.<br>';
}
if (strlen($message) < 2) {
$error_message .= 'The Message you entered do not appear to be valid.<br>';
}
if (strlen($error_message) > 0) {
problem($error_message);
}
$email_message = "Form details below.\n\n";
function clean_string($string)
{
$bad = array("content-type", "bcc:", "to:", "cc:", "href");
return str_replace($bad, "", $string);
}
$email_message .= "Name: " . clean_string($name) . "\n";
$email_message .= "Email: " . clean_string($email) . "\n";
$email_message .= "Message: " . clean_string($message) . "\n";
// create email headers
$headers = 'From: ' . $email . "\r\n" .
'Reply-To: ' . $email . "\r\n" .
'X-Mailer: PHP/' . phpversion();
#mail($email_to, $email_subject, $email_message, $headers);
?>
<!-- include your success message below -->
Thank you for contacting us. We will be in touch with you very soon.
<?php
}
?>
Are you installed a PHP server in your Computer?
If not, download WAMP and try again, you need access the page via http://localhost, and not via file://path_to_document in your web navigator.
Or run the script in a web server.
Related
I am trying out PHP after years of inactivity, and I thought I had it but looks like I lost the touch. Can anyone see why I might not be getting any data sent to my email address?
HTML code:
<form name="Call Back Request" id="request-call-form" action="callbackrequest.php" method="POST" novalidate="novalidate">
<div class="col-md-6">
<input id="name" type="text" placeholder="Name" name="name">
<input id="email" type="text" placeholder="Email" name="email">
</div>
<div class="col-md-6">
<input id="phone" type="text" placeholder="Phone" name="phone">
<input id="subject" type="text" placeholder="Subject" name="subject">
</div>
<div class="col-md-6">
<button type="submit" value="submit" class="thm-btn">submit now</button>
</div>
<div class="col-md-12">
<div id="success"></div>
</div>
</form>
PHP Code:
<?php
if (isset($_POST['email'])) {
$email_to = "xxxxx#xxxx.com";
$email_subject = "Call Back Request Form - Home Page | rootlayertechnologies.com.au";
function died($error) {
// your error code can go here
echo "We are very sorry, but there were error(s) found with the form you submitted. ";
echo "These errors appear below.<br /><br />";
echo $error . "<br /><br />";
echo "Please go back and fix these errors and submit the form again.<br /><br />";
die();
}
// validation expected data exists
if (!isset($_POST['name']) || !isset($_POST['email']) || !isset($_POST['phone']) || !isset($_POST['subject'])) {
died('We are sorry, but there appears to be a problem with the form you submitted.');
}
$first_name = $_POST['name']; // required
$email_from = $_POST['email']; // required
$telephone = $_POST['phone']; // not required
$comments = $_POST['subject']; // required
$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-]+#[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
if (!preg_match($email_exp, $email_from)) {
$error_message .= 'The Email Address you entered does not appear to be valid.<br />';
}
$string_exp = "/^[A-Za-z .'-]+$/";
if (!preg_match($string_exp, $name)) {
$error_message .= 'The Name you entered does not appear to be valid.<br />';
}
if (strlen($subject) < 2) {
$error_message .= 'The Subject you entered does not appear to be valid.<br />';
}
if (strlen($error_message) > 0) {
died($error_message);
}
$email_message = "Form details below.\n\n";
function clean_string($string) {
$bad = [
"content-type",
"bcc:",
"to:",
"cc:",
"href"
];
return str_replace($bad, "", $string);
}
$email_message .= "Name: " . clean_string($name) . "\n";
$email_message .= "Email: " . clean_string($email_from) . "\n";
$email_message .= "Phone: " . clean_string($phone) . "\n";
$email_message .= "Subject: " . clean_string($subject) . "\n";
// create email headers
$headers = 'From: ' . $email_from . "\r\n"
. 'Reply-To: ' . $email_from . "\r\n"
. 'X-Mailer: PHP/' . phpversion();
#mail($email_to, $email_subject, $email_message, $headers);
?>
<!-- include your own success html here -->
<p>Thanks for contacting us. We have received your request for a call back and a friendly member of our Product Solutions team will be in touch with you soon.</p>
<?php}?>
I would begin by removing the <a> tag inside the <submit> button. I think that by clicking the link, you may be just opening the page instead of submitting the form.
Then, on the PHP code, i would confirm that $email_to is properly set. (It may seem obvious, but it's always worth confirming.)
I couldn't test the code, but i didn't find any bugs per se. (Some things could be improved, e.g. lack of filter_var and perhaps html_escape, addslashes, or strip_tags.)
This leads me to think that the problem may be at the server level. So, here are some things to consider:
From: must be an address from the same domain as the site (e.g. info#rootlayertechnologies.com.au). Keep Reply-to: as is, though.
Ensure that the SPF record is properly set. You may also want to delve into DKIM and DMARC.
Some hosting companies disable PHP mail. You may need to implement SMTP.
I'm going to be upfront here. I don't really know PHP, but I want to use it to make a simple email form for my website that I'm building. I have put together what I thought would work, but I still get errors when I click submit.
Can anyone just tell me what I'm doing wrong to get it to work?
Here's the code (sorry if I over posted the code, I don't have the slightest clue where the problem is):
Contact.html:
<h1 class="cover-heading">Contact Me</h1>
<div class="form-area">
<form role="form" method="post" action="Contact.php">
<br style="clear:both">
<div class="form-group">
<input type="text" class="form-control" id="name" name="name" placeholder="Name"
required>
</div>
<div class="form-group">
<input type="text" class="form-control" id="email" name="email" placeholder="Email"
required>
</div>
<div class="form-group">
<input type="text" class="form-control" id="mobile" name="mobile" placeholder="Mobile Number"
required>
</div>
<div class="form-group">
<input type="text" class="form-control" id="subject" name="subject" placeholder="Subject"
required>
</div>
<div class="form-group">
<textarea class="form-control" type="textarea" name="message" id="message" placeholder="Message"
maxlength="1337" rows="7"></textarea>
<span class="help-block"><p id="characterLeft" class="help-block ">You have reached the limit</p></span>
</div>
<button type="submit" id="submit" name="submit" class="btn btn-lg btn-secondary ">Submit Form</button>
</form>
</div>
Contact.php:
<?php
if(isset($_POST['email'])) {
// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "christopherbare#outlook.com";
$email_subject = "ChristopherBare.com Contact form";
function died($error) {
// your error code can go here
echo "We are very sorry, but there were error(s) found with the form you submitted. ";
echo "These errors appear below.<br /><br />";
echo $error."<br /><br />";
echo "Please go back and fix these errors.<br /><br />";
die();
}
// validation expected data exists
if(!isset($_POST['name']) ||
!isset($_POST['subject']) ||
!isset($_POST['email']) ||
!isset($_POST['mobile']) ||
!isset($_POST['message'])) {
died('We are sorry, but there appears to be a problem with the form you submitted.');
}
$name = $_POST['name']; // required
$email = $_POST['email']; // required
$mobile = $_POST['mobile']; // not required
$subject = $_POST['subject']; //required
$message = $_POST['message']; // required
$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-]+#[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
if(!preg_match($email_exp,$email_from)) {
$error_message .= 'The Email Address you entered does not appear to be valid.<br />';
}
$string_exp = "/^[A-Za-z .'-]+$/";
if(!preg_match($string_exp,$first_name)) {
$error_message .= 'The First Name you entered does not appear to be valid.<br />';
}
if(!preg_match($string_exp,$last_name)) {
$error_message .= 'The Last Name you entered does not appear to be valid.<br />';
}
if(strlen($message) < 2) {
$error_message .= 'The message you entered does not appear to be valid.<br />';
}
if(strlen($error_message) > 0) {
died($error_message);
}
$email_message = "Form details below.\n\n";
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
$email_message .= "Name: ".clean_string($name)."\n";
$email_message .= "Email: ".clean_string($email)."\n";
$email_message .= "Mobile: ".clean_string($mobile)."\n";
$email_message .= "Subject: ".clean_string($subject)."\n";
$email_message .= "Comments: ".clean_string($message)."\n";
// create email headers
$headers = 'From: '.$email."\r\n".
'Reply-To: '.$email."\r\n" .
'X-Mailer: PHP/' . phpversion();
#mail($email_to, $email_subject, $email_message, $headers);
?>
<?php
}
?>
Here is the error:
"; echo $error."
"; echo "Please go back and fix these errors.
"; die(); } // validation expected data exists if(!isset($_POST['name']) || !isset($_POST['subject']) || !isset($_POST['email']) || !isset($_POST['mobile']) || !isset($_POST['message'])) { died('We are sorry, but there appears to be a problem with the form you submitted.'); } $name = $_POST['name']; // required $email = $_POST['email']; // required $mobile = $_POST['mobile']; // not required $subject = $_POST['subject']; //required $message = $_POST['message']; // required $error_message = ""; $email_exp = '/^[A-Za-z0-9._%-]+#[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/'; if(!preg_match($email_exp,$email_from)) { $error_message .= 'The Email Address you entered does not appear to be valid.
'; } $string_exp = "/^[A-Za-z .'-]+$/"; if(!preg_match($string_exp,$first_name)) { $error_message .= 'The First Name you entered does not appear to be valid.
'; } if(!preg_match($string_exp,$last_name)) { $error_message .= 'The Last Name you entered does not appear to be valid.
'; } if(strlen($message) < 2) { $error_message .= 'The message you entered does not appear to be valid.
'; } if(strlen($error_message) > 0) { died($error_message); } $email_message = "Form details below.\n\n"; function clean_string($string) { $bad = array("content-type","bcc:","to:","cc:","href"); return str_replace($bad,"",$string); } $email_message .= "Name: ".clean_string($name)."\n"; $email_message .= "Email: ".clean_string($email)."\n"; $email_message .= "Mobile: ".clean_string($mobile)."\n"; $email_message .= "Subject: ".clean_string($subject)."\n"; $email_message .= "Comments: ".clean_string($message)."\n"; // create email headers $headers = 'From: '.$email."\r\n". 'Reply-To: '.$email."\r\n" . 'X-Mailer: PHP/' . phpversion(); #mail($email_to, $email_subject, $email_message, $headers); ?>
Based on your error, look like you doesn't have PHP installed in your system.
You can download PHP here : http://php.net/downloads.php
If you are using Windows OS, I suggest you install xampp for easy use. You can download xampp here : https://www.apachefriends.org/index.html
I'm working on a PHP mail form and for some reason only certain text doesn't send through. It has me baffled and I can't even think of how to Google an answer.
For example, if I enter for the form:
Name: Indiana Jones
Number: 000
Email: indiana#jones.com
The mail function won't work, however if I change the 000 to 999, it works.
OR if I change the indiana#jones.com to indiana#jones.net, it suddenly works too.
Here are snippers, from my HTML:
<form method="post" action="mail.php">
Name<br />
<input style="width:878px;" type="text" name="name" id="name" required>
Best Contact Number<br />
<input style="width:415px;" type="text" name="number" id="number" required>
Email<br />
<input style="width:415px;" type="email" name="email" id="email" required>
</form>
And from my PHP (mail.php): (personal email addresses have been removed)
<?php
ini_set("SMTP","mail.email.com.au");
ini_set('sendmail_from', 'email#email.com.au');
$to = "email#email.com.au";
$subject = "Mail Form";
$message = "Name:" . "\n" . $_POST['name'] . "\n\n";
$message .= "Best Contact Number:" . "\n" . $_POST['number'] . "\n\n";
$message .= "Email:" . "\n" . $_POST['email'] . "\n\n";
$headers = "From: Removed <email#email.com.au> \n";
if(#mail($to, $subject, $message, $headers))
{
echo "<script type='text/javascript'>alert('Thank you for your submission'); window.location.href = 'index.html';</script>";}
else
{
echo "<script type='text/javascript'>alert('There was an error - please check all fields and try again'); window.location.href = 'index.html';</script>";
}
?>
well, I just got a code that will make it work great .
<?php
if(isset($_POST['email'])) {
// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "email#email.com.au";
$email_subject = "Mail Form";
function died($error) {
// your error code can go here
echo "<script type='text/javascript'>alert('There was an error because '); window.location.href = 'index.html';</script>";
die();
}
// validation expected data exists
if(!isset($_POST['name']) ||
!isset($_POST['number']) ||
!isset($_POST['email'])) {
died('<script type='text/javascript'>alert('There was an error - please check all fields and try again'); window.location.href = 'index.html';</script>');
}
$name = $_POST['name']; // required
$email = $_POST['email']; // required
$number = $_POST['number']; // required
$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-]+#[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
if(!preg_match($email_exp,$email_from)) {
$error_message .= 'The Email Address you entered does not appear to be valid.<br />';
}
$string_exp = "/^[A-Za-z .'-]+$/";
if(!preg_match($string_exp,$first_name)) {
$error_message .= 'The Name you entered does not appear to be valid.<br />';
}
if(strlen($comments) < 0) {
$error_message .= 'The Number you entered do not appear to be valid.<br />';
}
if(strlen($error_message) > 0) {
died($error_message);
}
$email_message = "Form details below.\n\n";
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
$email_message .= "Name: ".clean_string($name)."\n";
$email_message .= "Email: ".clean_string($email)."\n";
$email_message .= "Number: ".clean_string($number)."\n";
// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
#mail($email_to, $email_subject, $email_message, $headers);
?>
<!-- if sent successfuly -->
<script type='text/javascript'>alert('Thank you for your submission'); window.location.href = 'index.html';</script>
<?php
}
?>
You can add this code to display the errors for users
echo $error."<br /><br />";
Sorry, it turns out this was an Exchange server issue. I have no idea why it was letting some messages go through and filtering others, but our IT team sorted it out and had to allow the website's IP through.
Form is fully functional now though!
My form doesn't generate an E-Mail it just redirects me to a blank page.
i have my .php form in a folder named php on my server
thank you for your help.
here is my html code
<form id="form" method="post" action="php/send_form_email.php">
<fieldset>
<label><strong>Name:</strong>
<input type="text" value="">
</label>
<label><strong>Email:</strong>
<input type="text" value="">
</label>
<label><strong>Phone:</strong>
<input type="text" value="">
</label>
<label><strong>Message:</strong>
<textarea></textarea>
</label>
<div class="btns">ClearSend</div>
</fieldset>
</form>
here is the php code i'm using:
<?php
if(isset($_POST['email'])) {
// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "###.com";
$email_subject = "havok security contact form";
function died($error) {
// your error code can go here
echo "We are very sorry, but there was an error found with the form you submitted. ";
echo "These errors appear below.<br /><br />";
echo $error."<br /><br />";
echo "Please go back and fix these errors.<br /><br />";
die();
}
// validation expected data exists
if(!isset($_POST['name']) ||
!isset($_POST['email']) ||
!isset($_POST['phone']) ||
!isset($_POST['message'])) {
died('We are sorry, but there appears to be a problem with the form you submitted.');
}
$first_name = $_POST['name']; // required
$email_from = $_POST['email']; // required
$telephone = $_POST['phone']; // not required
$comments = $_POST['message']; // required
$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-]+#[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
if(!preg_match($email_exp,$email_from)) {
$error_message .= 'The Email Address you entered does not appear to be valid.<br />';
}
$string_exp = "/^[A-Za-z .'-]+$/";
if(!preg_match($string_exp,$name)) {
$error_message .= 'The Name you entered does not appear to be valid.<br />';
}
if(strlen($message) < 2) {
$error_message .= 'The Message you entered do not appear to be valid.<br />';
}
if(strlen($error_message) > 0) {
died($error_message);
}
$email_message = "Form details below.\n\n";
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
$email_message .= "Name: ".clean_string($first_name)."\n";
$email_message .= "Email: ".clean_string($email_from)."\n";
$email_message .= "Phone: ".clean_string($telephone)."\n";
$email_message .= "Message: ".clean_string($comments)."\n";
// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
#mail($email_to, $email_subject, $email_message, $headers);
?>
<!-- include your own success html here -->
Thank you for contacting us. We will be in touch with you very soon.
<?php
}
?>
The input type under email needs to have a name attribute called "email". Your PHP script is looking to see if $_POST["email"] is set, and it is not. So, this should work:
<form id="form" method="post" action="php/send_form_email.php">
<fieldset>
<label><strong>Name:</strong>
<input type="text" name="name" value="">
</label>
<label><strong>Email:</strong>
<input type="text" name="email" value="">
</label>
<label><strong>Phone:</strong>
<input type="text" name="phone" value="">
</label>
<label><strong>Message:</strong>
<textarea name = "message"></textarea>
</label>
<div class="btns">ClearSend</div>
</fieldset>
</form>
I am coding a personal website and having an issue with my contact from. If you can help me find what's wrong I would really appreciate it.
The link to the website is www.tiryakicreative.com and the code for the php form is given below:
<div id="form">
<form id="ajax-contact-form" action="contact_form/send_form_email.php…
<fieldset class="info_fieldset">
<div id="note"></div>
<div id="fields">
<label>Name</label>
<input class="textbox" type="text" name="name" value="" />
<label>E-Mail</label><input class="textbox" type="text" name="email" value="" />
<label>Subject</label>
<input class="textbox" type="text" name="subject" value="" />
<label>Message</label>
<textarea class="textbox2" name="message" rows="5" cols="25"></textarea>
<label> </label><input class="button" type="image" src="send2.gif" id="submit" Value="Send Message" />
</div>
</fieldset>
</form>
</div>
</div>
Here is the php code for the given html code:
<?php
if(isset($_POST['email'])) {
// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "ian_tiryaki#hotmail.com";
$email_subject = "New Email from Website";
function died($error) {
// ERROR CODE GOES HERE
echo "We are very sorry, but there were error(s) found with the form you submitted. ";
echo "These errors appear below.";
echo $error."";
echo "Please go back and fix these errors.";
die();
}
// validation expected data exists
if(!isset($_POST['name']) ||
!isset($_POST['email']) ||
!isset($_POST['subject']) ||
!isset($_POST['message'])) {
died('We are sorry, but there appears to be a problem with the form you submitted.');
}
$first_name = $_POST['name']; // required
$email_from = $_POST['email']; // required
$telephone = $_POST['subject']; // not required
$comments = $_POST['message']; // required
$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-]+#[A-Za-z0-9.-]+\.[A-Z…
if(!preg_match($email_exp,$email_from)) {
$error_message .= 'The Email Address you entered does not appear to be valid.';
}
$string_exp = "/^[A-Za-z .'-]+$/";
if(!preg_match($string_exp,$first_name)) {
$error_message .= 'The Name you entered does not appear to be valid.';
}
if(!preg_match($string_exp,$last_name)) {
$error_message .= 'The subject you entered does not appear to be valid.';
}
if(strlen($comments) < 2) {
$error_message .= 'The message you entered do not appear to be valid.';
}
if(strlen($error_message) > 0) {
died($error_message);
}
$email_message = "Form details below.\n\n";
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:",…
return str_replace($bad,"",$string);
}
$email_message .= "First Name: ".clean_string($first_name)."\n";
$email_message .= "Email: ".clean_string($email_from)."\n";
$email_message .= "Subject: ".clean_string($telephone)."\n";
$email_message .= "Message: ".clean_string($comments)."\n";
// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
#mail($email_to, $email_subject, $email_message, $headers);
?>
<!-- include success html here -->
Thank you for contacting us. We will be in touch with you very soon.
<?php
}
?>
Are you receiving the emails after the user sends the email?
Try adding
error_reporting(E_ALL);
to the top of your script.
You could also try removing the # from the mail command
mail($email_to, $email_subject, $email_message, $headers);
As this will be suppressing any errors that is being generated.
It could be something as simple as the PHP mail function having additional headers disabled (some hosts do this for security reasons) in which case the mail function will fail.
set this is in your action form ...
and you will definately get mail from here..
and although there is an error you may be use use below code for send mail using php without declare a variable...
like
$email_to=$_POST['email'];
$email_subject=$_POST['subject'];
$email_message=$_POST['message'];
$headers=$_POST['title'];
mail('$email_to', '$email_subject', '$email_message', '$headers');
otherwise
use below code for send mail
mail('$_POST['email']','$_POST['subject']','$_POST['message']','$_POST['title']');