Ok, so I've got a contact form:
<?php
if (isset($_POST['subtest'])) {
$to = 'thomofawsome#gmail.com';
$name = $_POST['firstname'];
$lastName = $_POST['lastname'];
$email = $_POST['email'];
$city = $_POST['city'];
$state = $_POST['state'];
$zip = $_POST['zip'];
$comments = $_POST['comments'];
$Message = <<< STOP
From: $name $lastName
Email: $email
In: $city, $state, $zip
Comments: $comments
STOP;
$subject = "Contact Request";
$headers = 'From: system';
if (mail($to, $subject, $Message, $headers)) {
echo '<div id="thanks">Mail sent</div>';
exit();
}
else {
echo 'Mail Failed';
}
}
?>
<form name="contact_form" action="" method="post">
<input type="hidden" name="subtest" value="true">
First Name:<br>
<input type="text" name="firstname">
<br>
Last Name:<br>
<input type="text" name="lastname">
<br>
Email Address:<br>
<input type="text" name="email">
<br>
City:<br>
<input type="text" name="city">
<br>
State:<br>
<input type="text" name="state">
<br>
Zip:<br>
<input type="text" name="zip">
<br>
Comments:<br>
<textarea name="comments"></textarea>
<br>
<input id="submit" type="submit" name="submit" value="Send">
<br>
</form>
The problem is I want the echo mail sent correctly formatted (with a green color, positioned correctly on the page, etc.). As you can see, I've put it in a div. When I submit the form though, I'm redirected back to the form page, except the entire form and footer disappear, and Mail sent appears on the bottom of the page (correctly formatted).
Any ideas?
The exit() function prevents the rest of the script from executing, which includes your form and footer. Remove it and it will work.
if (mail($to, $subject, $Message, $headers)) {
echo '<div id="thanks">Mail sent</div>';
} else {
echo 'Mail Failed';
}
As Raidenance pointed out, if someone refreshes this page after posting the form it will resend the email address. A better solution to this problem is to post your contact form to another url (/contact/submit for instance) and on completion of the script execution at that url simply redirect back to the contact form with a parameter
header("Location:/contact?success=true");
Then on your contact form page:
if (isset($_GET['success']) && $_GET['success'] == "true") {
echo '<div id="thanks">Mail sent</div>';
} else {
echo 'Mail Failed';
}
This will prevent the issue of the user reloading and receiving the email multiple times.
Related
I want to set a Dynamic Header on mail header when i send mail. I don't want to do it with SMPT server and if it will be in codeigniter, so it will be greate. You will get idea what exactly i want by given image bellow.
from: Google < dynamic email#gmail.com >
My Code
<?php
if(isset($_POST['send'])) {
//Email information
$email = $_POST['email'];
$subject = $_POST['subject'];
$comment = $_POST['comment'];
//send email
mail($email, "$subject", $comment, "From:" . $email);
//Email response
echo "Thank you for contacting us!";
}
//if "email" variable is not filled out, display the form
else {
?>
<form method="post" name="testmail">
Email: <input name="email" type="text" />
Subject: <input name="subject" type="text" />
Message:<textarea name="comment" rows="15" cols="40"></textarea>
<input type="submit" name="send" value="Submit" />
</form>
<?php } ?>
use like this:
mail(to,subject,message,headers,parameters);
Being so bad at PHP, I've decided to post here as a last resort.
I want to add a "who" variable to the message body of emails sent via PHP contact form. The form works fine for name, email and message but the "who" input I would like to be a part of the email message that comes through, as a way to communicate who is being referred.
I have tried to add $who=$_REQUEST['who']; as well as $who to the mail line but neither work, the latter doesn't even send an email at all.
<?php
$action=$_REQUEST['action'];
if ($action=="") /* display the contact form */
{
?>
<form action="" method="POST" enctype="multipart/form-data">
<input type="hidden" name="action" value="submit">
<input name="name" type="text" placeholder="Your Name" value="" size="14"/>
<input name="email" type="text" placeholder="Your Email" value="" size="14"/>
<textarea name="who" placeholder="Who should we contact?" rows="1" cols="14"></textarea>
<textarea name="message" placeholder="Description" rows="2" cols="14"></textarea><br>
<input type="submit" class="button special" value="SUBMIT"/>
</form>
<?php
}
else
{
$name=$_REQUEST['name'];
$email=$_REQUEST['email'];
$message=$_REQUEST['message'];
if (($name=="")||($email=="")||($message==""))
{
echo "All fields are required, please fill out the form again.";
}
else{
$from="From: $name<$email>\r\nReturn-path: $email";
$subject="Referral for ******* **";
mail("chris#********.com.au", $subject, $message, $from);
}
{
echo "<script type='text/javascript'>window.location.href ='../thanks.php';</script>";
}
}
?>
In PHP . is the concatenation operator which returns the concatenation of its right and left arguments
Try this
$name = $_REQUEST['name'];
$email = $_REQUEST['email'];
$message = $_REQUEST['message'] . "\n\rFrom: " . $_REQUEST['who'];
I am trying to make a contact form using PHP and some issue is there. I am new to PHP so couldn't figured it out. The form works if there is no validation code applied but as I apply validation code so that some fields can be made necessary, the form doesn't works right. Moreover when I leave any required field empty then them form doesn't show any error message. Can someone please tell what the problem is.
HTML Form
<form action="mail.php" method="POST" >
Name: <input type="text" name="name"><br/><br/>
Email: <input type="email" name="email"><br/><br/>
Phone Number: <input type="text" name="phone_number"><br/><br/>
Website: <input type="text" name="website"><br/><br/>
Message: <textarea name="message" rows="6" cols="25"></textarea><br/><br/>
<input type="submit" value="Submit">
</form>
Main PHP Script File
<?php
if(isset ($_POST['submit'])) {
$errors = array();
if(!empty ($_POST ['name'])) {
$name = $_POST ['name'];
} else {
$errors[] = "You forgot to enter your Name.";
}
if(!empty ($_POST ['email'])) {
$email = $_POST ['email'];
} else {
$errors[] = "You forgot to enter your Email.";
}
if(!empty ($_POST ['message'])) {
$message = $_POST ['message'];
} else {
$errors[] = "You forgot to enter your Message.";
}
$phone_number = $_POST['phone_number'];
$website = $_POST['website'];
$formcontent = "From: $name \n Email: $email \n Phone Number: $phone_number \n Website: $website \n Message: $message";
$recipient = "yourmail#emial.com";
$subject = "Contact Form";
$mailheader = "From: $email \r\n";
mail($formcontent, $recipient, $subject, $mailheader);
if(isset($_POST['submit'])) {
if(!empty($errors)) {
foreach ($errors as $msg)
{
echo '<li>'. $msg . '</li>';
}
} else {
echo "Thank You";
}
}
}
?>
UPDATE
Thanks for your replies guys, I literally forgot to have name attribute for submit button. That helped for showing some result. But now some notices are showing for undefined variables as email, message (if I provide only name in form and hit submit button) for $formcontent and $mailheader lines.
There are a few things wrong with your code.
if(isset ($_POST['submit'])) you have no name attribute for the submit input to support that, therefore nothing inside that conditional statement will be executed.
Having used error reporting, would have thrown an "Undefined index submit..." warning/notice.
So you need to add one:
<input type="submit" value="Submit" name="submit">
^^^^^^^^^^^^^
Then you have your mail() parameters which are not in the right order.
mail($formcontent, $recipient, $subject, $mailheader);
which should be:
To:
Subject:
Message:
Headers
Change it to:
mail($recipient, $subject, $formcontent, $mailheader);
For more information on mail(), visit the following link on PHP.net:
http://php.net/manual/en/function.mail.php
Edit:
You also need to place mail() function in a different place, where you have your "Thank you". Otherwise, even if an email address is not entered in the form, the mail would still be sent out, thus showing as "unknown sender" in the From. Placing mail() in the else if no errors are found.
if(isset($_POST['submit'])) {
if(!empty($errors)) {
foreach ($errors as $msg)
{
echo '<li>'. $msg . '</li>';
}
} else {
mail($recipient, $subject, $formcontent, $mailheader);
echo "Thank You";
}
}
Add error reporting to the top of your file(s) which will help find errors.
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
// rest of your code
Sidenote: Error reporting should only be done in staging, and never production.
You can also add Anti-spam to your form
$q1 = mt_rand(1,10);
$q2 = mt_rand(1,10);
$answer = $q1 + $q2;
<form action="mail.php" method="POST" >
Name: <input type="text" name="name"><br/><br/>
Email: <input type="email" name="email"><br/><br/>
Phone Number: <input type="text" name="phone_number"><br/><br/>
Website: <input type="text" name="website"><br/><br/>
Message: <textarea name="message" rows="6" cols="25"></textarea><br/><br/>
*What is <?php echo $q1 ." + ". $q2;?>? (Anti-spam):
<input type="number" required name="Human" ><br>
<!--question-->
<input name="answer" id="subject" type="hidden" value="<?php echo "$answer"; ?>">
<input type="submit" value="Submit">
</form>
in your form you can check if the answer is correct
<?php
$answer = $_POST['answer'];
if(isset ($_POST['submit']) && $_POST['human'] == answer) {
your mail procesing here
}
I have an html input form as well as a php email script that takes these values on the same page.
The problem is that before I submit any data into the forms I get a blank email because my php script is not waiting for the user input.
I don'y wan't to use another page for my email script because I don't want to pass variables through GET and I don't know how to implement sessions yet.
Thanks and here is my code
<div id = "center">
<form action="post.php" name="emailform" method="post">
<input type="text" name="name">
<input type="text" name="email">
<input type="text" name="message">
<input type="submit" value="Send Email">
</form>
</div>
<?php
if (!isset($_POST['submit'])) {
echo 'you have hit the submit button';
$name = $_POST['name'];
$visitor_email = $_POST['email'];
$message = $_POST['message'];
$email_from = 'trustyclient#yoursite.com';
$email_subject = "Message from client";
$email_body = "Message from: $visitor_email \n \n Message:$message";
$to = "myemail#myemail.com";
$headers = "from:adam\r\n";
mail($to,$email_subject,$email_body,$headers);
} else {
echo 'You have not hit the submit button yet';
}
?>
First, give your submit button a name, like 'submit' (because you've already referenced that name in the PHP). Example:
<input type="submit" name="submit" value="Send Email">
Now you can actually use $_POST['submit'] in your code.
Then another tweak:
When you state if (!isset($_POST['submit'])) {, the following code runs if the submit button has not been pressed, because of the !. To fix, just remove the !, making it:
if (isset($_POST['submit'])) {
! tells the if statement to evaluate to true if the following expression, here isset($_POST['submit']), evaluates to false. Therefore ! means "if the opposite".
NB: Also, the concept that the PHP runs when the submit button is pressed is slightly off. The submit button triggers that page to load a different page (or the same page). The PHP code runs only once when the page loads.
Try this.
<div id = "center">
<form action="post.php" name="emailform" method="post">
<input type="text" name="name">
<input type="text" name="email">
<input type="text" name="message">
<input type="submit" value="Send Email">
</form>
</div>
<?php
if (isset($_POST['submit'])) {
echo 'you have hit the submit button';
if (empty(trim($_POST['name'])) || empty(trim($_POST['email'])) || empty(trim($_POST['message']))) {
echo 'Some fields are empty.';
} else {
$name = $_POST['name'];
$visitor_email = $_POST['email'];
$message = $_POST['message'];
$email_from = 'trustyclient#yoursite.com';
$email_subject = "Message from client";
$email_body = "Message from: $visitor_email \n \n Message:$message";
$to = "myemail#myemail.com";
$headers = "from:adam\r\n";
mail($to,$email_subject,$email_body,$headers);
}
} else {
echo 'You have not hit the submit button yet';
}
?>
I have a simple html contact form, and the php script to send the emails. It's working good, but I want the result (The email has been sent...) to show in the same page, without changing the page. How can I do this?
HTML:
<form name="contact" action="includes/send.php" id="contact_form">
<input type="text" placeholder="Name" name="name" /> <br />
<input type="email" placeholder="Email Address" name="email" /> <br />
<textarea name="message" placeholder="Message" rows="8"></textarea> <br />
<input type="submit" name="submit" id="submit_btn" value="Send" />
</form>
PHP:
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$to = 'amar123syla#gmail.com';
$subject = 'Message from AMARSYLA.COM';
$message = 'FROM: '.$name.' Email: '.$email.'Message: '.$message;
$headers = 'From: amar123syla#gmail.com';
if (filter_var($email, FILTER_VALIDATE_EMAIL)) { // this line checks that we have a valid email address
mail($to, $subject, $message) or die('Error sending Mail'); //This method sends the mail.
echo "Your email was sent!"; // success message
}
?>
jQuery.post('email.php', score, function(result) {
jQuery('#textBlock').html(result);
});
Something like that should work(Result is the echo in your php in this case)
A preffered method is to encode your php result to json so you've got a little more control over it in Javascript though.
jQuery.post('email.php', score, function(result) {
if (result.result == true) {
jQuery('#textBlock').html(result.text);
}
});
And in the email.php:
$result['result'] = true;
$result['text'] = 'Message has been sent';
header('Content-Type: application/json');
echo json_encode($result);
Here see if this works:
<form name="contact" action="**submit to same page**" id="contact_form">
<input type="submit" name="submit" id="submit_btn" value="Send" />
</form>
Then for the php (on the same page as the email form) just add if submit
<?php
if(isset($_POST['submit'])){
run your email script, add javascript, etc.
echo "Your email was sent!"; // success message
}
}
?>