I am a beginner in PHP. I am stuck on something that seems really easy.
I built a website, and there is a page in the website where people can send me e-mail through a form. The problem is, I think form seems to fail creating $POST_['name']. Just read the codes and you'll understand what im saying.
<form action="?" method="post">
<div id="right">
<label for="name"> <input type="text" class="kutu" name="name" ></label><br />
<label for="email"> <input type="email" class="kutu" name="email" ></label><br />
<label for="subject"> <input type="text" class="kutu" name="subject" ></label><br />
</div>
<div id="left">
<p>Name: </p>
<p>Your Email Adress: </p>
<p>Subject: </p>
</div>
<div id="bottom">
<p>Your Message:</p><br />
<label for="message"><textarea name="message" rows="3" cols="60"> </textarea></label>
</div>
<input type="submit" id="tus" value="submit">
</form>
And here is the PHP
if (isset($POST_['name'])) {
$email = $POST_['email'];
$subject = $POST_['subject'];
$message = "From: " . $POST_['name'] . ", " . $POST_['email'] . "\n Message: " . $POST_['message'];
try {
mail("myEmailAdress#gmail.com", $subject, $message, " ");
unset($POST_['name']);
header("Location: success.php");
}
catch (PDOException $e)
{
include 'index.html';
exit();
}
exit();
}
include 'contact2.php';
When i submit my form, it takes me to contact2.php again, which tells me that POST_['name'] was never created in the first place.
The variable name is $_POST, not $POST_. See http://php.net/EN/reserved.variables.post.php
Related
This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 5 years ago.
I haven't written code in years, so I am going off old code from a project that is over 5 years old, so I am not surprised that it doesn't work; I would like some pointers on how to make it work, please.
Here is what I have in my HTML email form --
<form action="fydcontact.php" method="post">
<div class="form-group">
<!--<label for="contact_name">Name:</label>-->
<input type="text" id="contact_name" class="form-control" placeholder="Name" />
</div>
<div class="form-group">
<!--<label for="contact_email">Email:</label>-->
<input type="text" id="contact_email" class="form-control" placeholder="Email Address" />
</div>
<div class="form-group">
<!--<label for="contact_message">Message:</label>-->
<textarea id="contact_message" class="form-control" rows="9" placeholder="Write a message"></textarea>
</div>
<button type="submit" class="btn btn-primary">Send</button>
</form>
and here is what my PHP looks like --
<?php
if(isset($_POST['send'])) {
// Prepare the email
$to = ''foryourdayformals#gmail.com ';
$name = $_POST['contact_name'];
$mail_from = $_POST['contact_email'];
$subject = 'Message sent from website';
$message = $_POST['contact_message'];
$header = "From: $name <$mail_from>";
// Send it
$sent = mail($to, $subject, $message, $header);
if($sent) {
echo 'Your message has been sent successfully! Return to For Your Day, LLC Website';
} else {
echo 'Sorry, your message could not send. Please use direct email link on Contact Us page (below the map). Return to For Your Day, LLC Website';
}
}
?>
Any help is GREATLY appreciated! Thanks!
update
$to = ''foryourdayformals#gmail.com ';
to
$to = 'foryourdayformals#gmail.com';
and need to add name attribute for form . This update your form
<form action="fydcontact.php" method="post">
<div class="form-group">
<!--<label for="contact_name">Name:</label>-->
<input type="text" id="contact_name" name="contact_name" class="form-control" placeholder="Name" />
</div>
<div class="form-group">
<!--<label for="contact_email">Email:</label>-->
<input type="text" id="contact_email" name="contact_email" class="form-control" placeholder="Email Address" />
</div>
<div class="form-group">
<!--<label for="contact_message">Message:</label>-->
<textarea id="contact_message" name="contact_message" class="form-control" rows="9" placeholder="Write a message"></textarea>
</div>
<button type="submit" class="btn btn-primary" name ="send">Send</button>
</form>
I have this simple email form using the POST function with a PHP mailto() function. I have PHP lines within my form as values to the fields. It takes way too long to send. Any help is appreciated.
PHP:
<?php
if ($_POST['submit']) {
if (!$_POST['name']) {
$error="<br/>-Please enter your name";
}
if (!$_POST['email']) {
$error.="<br/>-Please enter your email";
}
if (!$_POST['message']) {
$error.="<br/>-Please enter a message";
}
if (!$_POST['check']) {
$error.="<br/>-Please confirm you're real";
}
if ($error){
$result="Ohh no, somethings not right... $error";
}
else {
mail("email#email.com","website contact form","Name: ".$_POST['name']."
Email: ".$_POST['email']."
Message: ".$_POST['message']);
{
$result="Thank you, I will reply shortly.";
}
}
}
?>
HTML:
<form action="#contactme" method="post" role="form">
<div class="form-group">
<input type="text" name="name" placeholder="Enter your name" class="form-control form-control-lg" value="<? echo $_POST['name']; ?>" >
</div>
<div class="form-group">
<input type="email" name="email" placeholder="Enter your email" class="form-control form-control-lg" value="<? echo $_POST['email']; ?>">
</div>
<div class="form-group">
<textarea name="message" rows="5" class="form-control" placeholder="Your message..."><? echo $_POST['message']; ?> </textarea>
</div>
<div class="checkbox">
<label>
<input type="checkbox" name="check"><span class="real"> I am real</span>
</label>
</div>
<div align="center">
<input type="submit" name="submit" class="btn btn-secondary btn-lg" value="Send">
</div>
</form>
I have done everything in a query now it should be sent faster but it can also be that this code has an error but the first query is correct from the structure.
<?php
if ($_POST['submit']) {
if (!$_POST['name'] || !$_POST['email'] || !$_POST['message'] || !$_POST['check'] ||) {
$error="<br/>-Please enter";
}else
{
if ($error){
$result="Ohh no, somethings not right... $error";
}
else {
mail("email#email.com","website contact form","Name: ".$_POST['name']."
Email: ".$_POST['email']."
Message: ".$_POST['message']);
{
$result="Thank you, I will reply shortly.";
}
} <-- If you have a mistake, take a clip away.
}
}
?>
I'm not very good at PHP (more of a front-end developer) and I'm doing a website for a client. I got everything working up to now except for one thing. When the user fills out the contact form and submits it successfully i want to display a message that tells them the email was sent. I already have the div set up I just don't know who to execute it with PHP.
<div class="sent">Email has been sent.</div>
<form action="" method="POST" class="contact-form">
<img class="close" src="images/close-icon.png">
<div class="top-title">Contact Us</div>
<input type="hidden" name="action" value="submit">
<div class="input-title">Name*</div>
<input class="input" name="name" type="text" value="" size="30" required><br>
<div class="input-title">Phone Number*</div>
<input class="input" name="phone" type="text" value="" size="30" required><br>
<div class="input-title">Email Address*</div>
<input class="input" name="email" type="text" value="" size="30" required><br>
<div class="input-title">How did you hear about us?</div>
<input class="input" name="company" type="text" value="" size="30"/><br>
<div class="input-title">Let us know how we can help you</div>
<textarea class="textarea" name="message" rows="7" cols="30"></textarea><br>
<div class="required">*Fill is required</div>
<input class="submit-button" type="submit" value="Send Email"/>
</form>
<?php
{
$name=$_POST['name'];
$email=$_POST['email'];
$phone=$_POST['phone'];
$company=$_POST['company'];
$message= "\r\nName: " . $_POST['name'] ."\r\nPhone Number: ". $_POST['phone']."\r\nEmail: ". $_POST['email'] ."\r\nHow you know the company: ".$_POST['company'] ."\r\nReason for message: ".$_POST['message'];
$from="From: $name<$email>\r\nReturn-path: $email";
$subject="GS Website Message";
mail("name#email.com", $subject, $message, $from);
}
?>
Change: (and using a conditional statement)
mail("name#email.com", $subject, $message, $from);
to:
if(mail("name#email.com", $subject, $message, $from)){
$sent = "Message sent";
}
Then modify:
<div class="sent">Email has been sent.</div>
to read as:
<div class="sent"><?php if(!empty($sent)) { echo $sent; } ?></div>
Seeing you're new to PHP/forms, it's best to also check if your inputs are empty or not, otherwise you risk in either not getting mail, or receiving empty data.
http://php.net/manual/en/function.empty.php
Set inside a conditional statement also.
I.e.: using NOT empty: ! is the NOT operator in PHP.
if(!empty($_POST['var'])){...}
Edit:
The logic needs to be reversed.
First, place your PHP above your HTML form.
I also added a submit name attribute to your submit button, with a conditional statement and a ternary operator in the div.
<?php
if(isset($_POST['submit'])){
$name=$_POST['name'];
$email=$_POST['email'];
$phone=$_POST['phone'];
$company=$_POST['company'];
$message= "\r\nName: " . $_POST['name'] ."\r\nPhone Number: ". $_POST['phone']."\r\nEmail: ". $_POST['email'] ."\r\nHow you know the company: ".$_POST['company'] ."\r\nReason for message: ".$_POST['message'];
$from="From: $name<$email>\r\nReturn-path: $email";
$subject="GS Website Message";
if(mail("email#example.com", $subject, $message, $from)){
$sent = "Message sent";
}
}
?>
<div class="sent"><?php echo isset($sent) ? $sent : ''; ?></div>
<form action="" method="POST" class="contact-form">
<img class="close" src="images/close-icon.png">
<div class="top-title">Contact Us</div>
<input type="hidden" name="action" value="submit">
<div class="input-title">Name*</div>
<input class="input" name="name" type="text" value="" size="30" required><br>
<div class="input-title">Phone Number*</div>
<input class="input" name="phone" type="text" value="" size="30" required><br>
<div class="input-title">Email Address*</div>
<input class="input" name="email" type="text" value="" size="30" required><br>
<div class="input-title">How did you hear about us?</div>
<input class="input" name="company" type="text" value="" size="30"/><br>
<div class="input-title">Let us know how we can help you</div>
<textarea class="textarea" name="message" rows="7" cols="30"></textarea><br>
<div class="required">*Fill is required</div>
<input class="submit-button" type="submit" value="Send Email" name="submit"/>
</form>
$password=md5($_POST ['password']);
$password3 =md5($_POST ['password2']);
if($password != $password3)
{
$message = "Passwords do not match";
}
etc.
you need add
<form>
<div class="form-group">
<input type="password" name="password" id="password" class="form-control" placeholder="Password *" value="" required/>
</div>
<div class="form-group">
<div class="message"><?php if(!empty($message)) { echo $message; } ?></div>
<input type="password" name="password2" class="form-control" id="password2" placeholder="Confirm Password *" value="" required/>
</div>
</form>
I am working on my version 2 of my portfolio site, I had a working mailer I created with a guide about a year ago, transferred it, and can't get my page to display now.
Here is the code I am using:
<?php
if (isset($_REQUEST['email']))
//if "email" is filled out, send email
{
//send email
$name = $_REQUEST['name'] ;
$email = $_REQUEST['email'] ;
$subject = $_REQUEST["My_Portfolio_Website"] ;
$message = $_REQUEST['message'] . "\nName: " . $name . "\nEmail: ".$email; "\n \nMessage: ".$message;
echo "<h1>Thank you for contacting me. I will get back to you ASAP!</h1>";
}else{
//if "email" is not filled out, display the form
echo <form method="post" action="contact.php" class="connect">
<div>
<input id="name" name="name" type="text" required>
<label for="name">Your Name</label>
</div>
<div>
<input id="email" name="email" type="text" required>
<label for="email">Your Email</label>
</div>
<div>
<textarea id="message" name="message" required></textarea>
<label for="message">Your Message</label>
</div>
<div class="metro">
<div class="metro-button" type="submit">Click me</div>
</div>
</form>
}
?>
I ran it through a PHP syntax checker and it did not pull anything out, does anyone have any ideas?
For reference: Here is the original code I used on Version 1. I formatted it so it would display the email a little more cleaner, which probably royally screwed it up.
<?php
if (isset($_REQUEST['email']))
//if "email" is filled out, send email
{
//send email
$name = $_REQUEST['name'] ;
$email = $_REQUEST['email'] ;
$subject = $_REQUEST["My Portfolio Website"] ;
$message = $_REQUEST['message'].", Name: ".$name.", ".$phone.", Email: ".$email;
mail("TylerJStelmach#gmail.com", $subject, $message, "From:" . $email);
echo "<h1>Thank you for contacting me. I will get back to you ASAP!</h1>";
}else{
//if "email" is not filled out, display the form
echo "<form method='post' action='index.php'>
<input type='text' input name='name' id='name' class='contacttext' placeholder=' Your Name' required>
<input type='text' input name='email' id='email' class='contacttext' placeholder=' Your Email Address' required>
<textarea input type='text' name='message' id='message' class='contacttext' placeholder=' Your Message' cols='55' rows='5' required></textarea>
<input type='submit' id='submit' class='submitcontacttext' value='Send Message'>
</form>";
}
?>
There are some syntax errors.
Also, your snipplet does not contain code to send mail.
Corrected code is as following:
<?php
if (isset($_REQUEST['email']))
//if "email" is filled out, send email
{
//send email
$name = $_REQUEST['name'] ;
$email = $_REQUEST['email'] ;
$subject = $_REQUEST["My_Portfolio_Website"] ;
$message = $_REQUEST['message'] . "\nName: " . $name . "\nEmail: ".$email; "\n \nMessage: ".$message;
echo "<h1>Thank you for contacting me. I will get back to you ASAP!</h1>";
}else{
//if "email" is not filled out, display the form
echo '<form method="post" action="contact.php" class="connect">
<div>
<input id="name" name="name" type="text" required>
<label for="name">Your Name</label>
</div>
<div>
<input id="email" name="email" type="text" required>
<label for="email">Your Email</label>
</div>
<div>
<textarea id="message" name="message" required></textarea>
<label for="message">Your Message</label>
</div>
<div class="metro">
<div class="metro-button" type="submit">Click me</div>
</div>
</form>';
}
?>
as all are saying where is the line to send mail but as i have seen your site there is no sign of any code.
as i am just guessing, even if the if is false, the else should run as it is running in my localhost.
the problem must be something with your sever.
put a error_reporting(E_ALL); on top it will show errors if it there.
else try writing in a new file.
<?php
error_reporting(E_ALL);
if (isset($_REQUEST['email']))
//if "email" is filled out, send email
{
//send email
$name = $_REQUEST['name'] ;
$email = $_REQUEST['email'] ;
$subject = $_REQUEST["My_Portfolio_Website"] ;
$message = $_REQUEST['message'] . "\nName: " . $name . "\nEmail: ".$email; "\n \nMessage: ".$message;
echo "<h1>Thank you for contacting me. I will get back to you ASAP!</h1>";
}else{
?>
<form method="post" action="contact.php" class="connect">
<div>
<input id="name" name="name" type="text" required>
<label for="name">Your Name</label>
</div>
<div>
<input id="email" name="email" type="text" required>
<label for="email">Your Email</label>
</div>
<div>
<textarea id="message" name="message" required></textarea>
<label for="message">Your Message</label>
</div>
<div class="metro">
<div class="metro-button" type="submit">Click me</div>
</div>
</form>
<?php
}
?>
I was able to figure this out by re-writing the original code I had based on the guide I used about a year ago, I came up with this for the answer which indeed works.
For all of the handling of where the information goes and how it was formated I included this chunk of php before I declared the doctype.
<?php
if($_POST["submit"]) {
$recipient="myemail#email.com";
$subject="Client Mail";
$name=$_POST["name"];
$email=$_POST["email"];
$email_from = $_POST['email'];
$message=$_POST["message"];
$mailBody="Name: $name\nEmail: $email\n\nMessage: $message";
mail($recipient, $subject, $mailBody, "From: $sender <$email>");
$thankYou="<p class='thank-you'>Thank you! Your message has been sent.</p>";
}
?>
From here, I figured out that the reason it wasn't submitting to it's own page was because my submit button was neither a button or input. I had it set as a div so with some tweaks here and there I rewrote the lower half to include an input as the submit. Which left me with this:
<?=$thankYou ?>
I included a small amount of text to display ( the $thankYou ) when you submit to the page and it reloads.
<form method="post" action="contact.php" class="connect">
<div>
<input id="name" name="name" type="text" required>
<label for="name">Your Name</label>
</div>
<div>
<input id="email" name="email" type="text" required>
<label for="email">Your Email</label>
</div>
<div>
<textarea id="message" name="message" required></textarea>
<label for="message">Your Message</label>
</div>
<div class="metro">
<input type="submit" name="submit" class="metro-button submit-me">
</div>
</form>
So now when submitted, the email comes in with:
The 'from' being set by $email
The 'subject' being set by $subject
and the message ($message) being formatted in this manner:
Name: John Doe
Email: JDoe#fake.com
Message: This is John Doe's message.
My apologies for the terrible question phrasing before hand, it was late at night and I was getting frustrated and losing my place, got a good nights rest and was able to solve it.
Here is the live version, I encourage you to test it, it works for me perfectly, hopefully this snippet can help someone else in the future!
I've been looking around everywhere and cannot seem to find how to make this work - it is simply not sending an email to my address. Here is my HTML form:
<form id="contactMe" name="contact" method="post" novalidate="novalidate">
<fieldset>
<label for="name" id="name">Name<span class="required">*</span></label>
<input type="text" name="name" id="name" size="30" value="" required="">
<label for="email" id="email">Email<span class="required">*</span></label>
<input type="text" name="email" id="email" size="30" value="" required="">
<label for="phone" id="phone">Phone</label>
<input type="text" name="phone" id="phone" size="30" value="">
<label for="Message" id="message">Message<span class="required">*</span></label>
<textarea name="message" id="message" required=""></textarea>
<label for="Answer" id="answer">Name the house pet that says “<i>woof</i>“<span class="required">*</span></label>
<input type="text" name="answer" value="" required=""></br>
<input id="submit" type="submit" name="submit" value="Send">
</fieldset>
<div id="success">
<span class="green textcenter">
<p>Your message was sent successfully! I will be in touch as soon as I can.</p>
</span>
</div> <!-- closes success div -->
<div id="error">
<span><p>Something went wrong, try refreshing and submitting the form again.</p></span>
</div> <!--close error div-->
</form>
and here is my PHP saved as mailer.php:
<?php
$to = "someone#gmail.com";
$from = $_REQUEST['email'];
$name = $_REQUEST['name'];
$headers = "From: $from";
$subject = "You have a message from your.";
$fields = array();
$fields{"name"} = "name";
$fields{"email"} = "email";
$fields{"phone"} = "phone";
$fields{"message"} = "message";
$body = "Here is what was sent:\n\n"; foreach($fields as $a => $b){ $body .= sprintf("%20s: %s\n",$b,$_REQUEST[$a]); }
mail("maytee.kneitz#gmail.com",$subject,$message,"From: $from\n");
echo 'Mail sent';
?>
This is my first shot at working on a mailer / contact form, so sorry if it's a blatant problem. I just can't seem to find it. Any guidance would be appreciated.
I do have validation in my scripts (not posted here).
You don't have a form action defined. Try this:
<form id="contactMe" name="contact" method="post" action="mailer.php" novalidate="novalidate">
By default, the form will be submitted to its current location unless otherwise specified. In your case, point it to wherever your mail script is located