I need help, i'm trying to set up a contact form, I have the code and think I have it set up correctly with HTML and PHP. When I submit to test it on my testsite it send an email through but the email is blank.
Thanks in advance if you can help me please.
HTML:
<div class="col-md-6 contact-right">
<form name="contactform" action="mailer.php" method="post" role="form" enctype="text/plain">
<div class="styled-input agile-styled-input-top">
<input type="text" class="form-control" name="name" placeholder="Your Name">
<label for="name">Name</label>
<span></span>
</div>
<div class="styled-input">
<input type="text" class="form-control" name="email" placeholder="Your Email">
<label for="email">Email</label>
<span></span>
</div>
<div class="styled-input">
<input type="text" class="form-control" name="subject" placeholder="Subject">
<label for="subject">Subject</label>
<span></span>
</div>
<div class="styled-input">
<textarea class="form-control" rows="4" name="message" placeholder="Your message..."></textarea>
<label for="message">Message</label>
<span></span>
</div>
<input type="submit" value="Submit" name="submit">
</form>
</div>
<div class="clearfix"> </div>
</div>
</div>
</div>
PHP:
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$formcontent="From: $name \n Message: $message";
$recipient = "laura#ironcladdesign.co.uk";
$subject = "Enquiry from CK9C Website";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
echo "Thank You! We will be in touch very soon";
?>
The problem is here:
<form name="contactform" action="mailer.php" method="post" role="form" enctype="text/plain">
You are encoding as "text/plain". Replace with "application/x-www-form-urlencoded" which is the default or don't include enctype and that will be the default.
you can try it in place of $_POST
if ((!is_array($_POST)) || (count($_POST) < 1)) {
$_POST = json_decode(file_get_contents('php://input'), true);
}
Related
I'm new to website building and was trying to get a contact form fully functional but for some reason, it's not sending any e-mails nor is it redirecting me back to the homepage. Would anyone be able to please tell me where I went wrong?
This is the HTML code:
<form action="form_contact.php" method="POST">
<div class="row">
<div class="input-group">
<input type="text" name="name" id="name" required>
<label for="name">...</label>
</div>
<div class="input-group">
<input type="text" name="number" id="number" required>
<label for="number">...</label>
</div>
</div>
<div class="input-group">
<input type="text" name="email" id="email" required>
<label for="email">...</label>
</div>
<div class="input-group">
<textarea id="message" name="message" rows="8" required></textarea>
<label for="message">...</label>
<input type="text" id="website" name="website"/>
</div>
<button type="submit" value="1" id="submit">...</button>
</form>
This is the PHP code:
<?php
if (isset($_POST['submit'])){
$nm = $_POST['name'];
$nb = $_POST['number'];
$mailFrom = $_POST['email'];
$msg = $_POST['message'];
if(!empty($_POST['website'])) die();
$mailTo='myemail#mail.com';
$headers= 'From: '.$mailFrom;
$txt= 'You have received a new e-mail from
'.$nm.'.\n\n'.$msg.'.\n\n'.$nb;
$mailSent=mail($mailTo, $txt, $headers);
if ($mailSent){
header('Location: mywebsite.com');
}}
I would greatly appreciate any help. Thank you.
I tried deleting the "website" field in the form since it's not even visible but that also didn't help.
On clicking the submit button it is showing submit is not set after the if condition. I guess this message is showing because the form is not getting submitted. I don't know where I'm going wrong kindly help me fix this issue. I'm herewith attaching a part of my contact.php code and mail.php code. Thank you
contact.php
<form id="contact-form" method="POST" action="mail.php">
<div class="row">
<div class="col-md-4">
<div class="form-group">
<label>Name</label>
<input class="form-control" name="name" id="name" placeholder="" type="text" required>
</div>
</div>
<div class="col-md-4">
<div class="form-group">
<label>Email</label>
<input class="form-control" name="email" id="email" placeholder="" type="email" required>
</div>
</div>
<div class="col-md-4">
<div class="form-group">
<label>Subject</label>
<input class="form-control" name="subject" id="subject" placeholder="" required>
</div>
</div>
</div>
<div class="form-group">
<label>Message</label>
<textarea class="form-control" name="message" id="message" placeholder="" rows="10" required></textarea>
</div>
<div class="text-right"><br>
<input class="btn btn-primary solid blank button" id="btn" type="submit" value="submit" name="submit">
</div>
</form>
mail.php
<?php
if (isset($_POST['email'])){
echo "submit is set to {$_POST['submit']} and now we send the email<br>";
$to = "abc#email.com";
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$message .="\r\n from: $email";
if(mail ($to, $subject, $name, $message)){
echo "Enquiry sent successfully!<br>";
}
else
{
echo "Mail was not sent. Please try again later<br>";
}
} else{
echo"submit is not set<br>";
}
echo "after the if condition";
header('Location: https://imatrixautomation.com/contact.php');
exit;
?>
nothing wrong with your code, i guess you doing a direct request to mail.php
try this code, maybe this help your problem
<?php
if($_POST){ // add condition if http request is POST then process your POST request
if (isset($_POST['email'])){
echo "submit is set to {$_POST['submit']} and now we send the email<br>";
$to = "abc#email.com";
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$message .="\r\n from: $email";
if(mail ($to, $subject, $name, $message)){
echo "Enquiry sent successfully!<br>";
}
else
{
echo "Mail was not sent. Please try again later<br>";
}
} else{
echo"submit is not set<br>";
}
echo "after the if condition";
exit; // if POST process has done, script will stop, and code above will not showing
}
?>
<form id="contact-form" method="POST" action="">
<div class="row">
<div class="col-md-4">
<div class="form-group">
<label>Name</label>
<input class="form-control" name="name" id="name" placeholder="" type="text" required>
</div>
</div>
<div class="col-md-4">
<div class="form-group">
<label>Email</label>
<input class="form-control" name="email" id="email" placeholder="" type="email" required>
</div>
</div>
<div class="col-md-4">
<div class="form-group">
<label>Subject</label>
<input class="form-control" name="subject" id="subject" placeholder="" required>
</div>
</div>
</div>
<div class="form-group">
<label>Message</label>
<textarea class="form-control" name="message" id="message" placeholder="" rows="10" required></textarea>
</div>
<div class="text-right"><br>
<input class="btn btn-primary solid blank button" id="btn" type="submit" value="submit" name="submit">
</div>
</form>
How to add receiver address to my contact form. I tried and searched a lot but i cant solve the issue by myself. help! Thx!!!
<article id="contact">
<h2 class="major">Contact</h2>
<form name="contactform" method="post" action="index.html">
<div class="fields">
<div class="field half">
<label for="name">Name</label>
<input type="text" name="name" id="name" />
</div>
<div class="field half">
<label for="email">Email</label>
<input type="text" name="email" id="email" />
</div>
<div class="field half">
<label for="email">Telefon</label>
<input type="text" name="telefon" id="email" />
</div>
<div class="field half">
<label for="email">Subject</label>
<input type="text" name="subject" id="email" />
</div>
<div class="field">
<label for="message">Message</label>
<textarea name="message" id="message" rows="4"></textarea>
</div>
</div>
<ul class="actions">
<li><input type="submit" value="Send Message" class="primary"/></li>
<li><input type="reset" value="Reset" /></li>
</ul>
</form>
</article>
Receiver mailId add at php side, you can pass receiver mailId as "to" filed in php mail function.
mail($to,$subject,$txt,$headers);
If you implement your sending mail in other way then send your php code
You need to update your action in form element
<form name="contactform" method="post" action="some_php_page.php">
and in some_php_page.php you need to add bellow code.
if(isset($_Post['name']){
$to = "Receiver Email Id ";
$subject = "Your Subject";
$name = trim($_POST['name']);
$email = $_Post['email'];
$telefon = $_Post['telefon'];
$subject = $_Post['subject'];
$message = $_Post['message'];
$body = "Name: ".$name.'\n';
$body .= "Email: ".$email.'\n';
$body .= "Subject: ".$subject.'\n';
$body .= "Message: ".$message.'\n';
mail($to,$subject,$body);
}
Hope this will help you...
I have a created a html form and a php file (mail.php) and i have added this in my form <form action="mail.php" method="POST">
but When I try to submit the form, instead of emailing me and providing the thank you message, it downloads the php page. When I run the .html page in my browser, it shows up fine but when I click send it downloads mail.php, instead of working.
Any suggestions?
ok this is the code
mail.php:
$name = $_POST['name'];
$email = $_POST['email'];
$telephone = $_POST['telephone'];
$comments = $_POST['comments'];
$formcontent=" From: $name \n telephone: $telephone \n comments: $comments";
$recipient = "lllkk#gmail.com";
$subject = "Contact Form";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
echo "Thank You!" ;
test.html:
<form action="mail.php" method="POST">
<div class="row">
<div class="form-group col-md-8 col-md-offset-1">
<input type="text" name="name" placeholder="Name" class="form-control">
</div>
<div class="form-group col-md-8 col-md-offset-1">
<input type="email" name="email" placeholder="Email" class="form-control">
</div>
<div class="form-group col-md-8 col-md-offset-1">
<input type="tel" name="telephone" placeholder="telephone" class="form-control">
</div>
<div class="clearfix"></div>
<div class="form-group col-md-8 col-md-offset-1">
<textarea class="form-control" name="comments" placeholder="Comments" rows="6"></textarea>
</div>
</div>
<input type="submit" value="Send"><input type="reset" value="Clear">
</form>
I,m trying to make a form send an e-mail but im getting page not found error.
This is the form itself
<form action="sendmail.php" method="post">
<div>
<div class="row half">
<div class="6u">
<input type="text" class="text" name="name" placeholder="Name" />
</div>
<div class="6u">
<input type="text" class="text" name="email" placeholder="Email" />
</div>
</div>
<div class="row half">
<div class="12u">
<input type="text" class="text" name="subject" placeholder="Subject" />
</div>
</div>
<div class="row half">
<div class="12u">
<textarea name="message" placeholder="Message"></textarea>
</div>
</div>
<div class="row">
<div class="12u">
<input type="submit" class="button" value="Send Message" />
</div>
</div>
</div>
</form>
And this is the PHP file named sendmail.php
<?php
$to = "info#aroundgalaxy.pt";
$email = $_REQUEST['email'] ;
$name = $_REQUEST['name'] ;
//$site = $_REQUEST['site'] ;
$subject = $_REQUEST['subject'] ;
$message = $_REQUEST['message'] ;
$headers = "noreply#aroundgalaxy.pt";
$body = "From: $name \n\n Email: $email \n\n Message: $message";
$sent = mail($to, $subject, $body, $headers) ;
if($sent)
{echo "<script language=javascript>window.location = 'LINK BACK TO CONTACT PAGE';</script>";}
else
{echo "<script language=javascript>window.location = 'LINK BACK TO CONTACT PAGE';</script>";}
?>
Am i missing something?
Thanks
The code should work. I dont think there is a problem with the code. Just make sure both the form page & sendmail.php are on the same directory