weird problem here, my html form disapears from the page when I click the send button regardless of success or not.
I display an alert box to indicate if the email was sent or not.
here is the code
<?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 class="champTextFormulaire" placeholder="Votre Nom" name="name" type="text" value="" size="30"/><br>
<input class="champTextFormulaire" placeholder="Votre email" name="email" type="text" value="" size="30"/><br>
<textarea id="champMessage" placeholder="Votre Message..." name="message" rows="7" cols="30"></textarea><br>
<input class="btnEnvoiFormulaire" type="submit" value="Envoi"/>
</form>
<?php
} else /* send the submitted data */ {
$name = $_REQUEST['name'];
$email = $_REQUEST['email'];
$message = $_REQUEST['message'];
if (($name == "") || ($email == "") || ($message == "")) {
print '<script type="text/javascript">';
print 'alert("Veuillez remplir tout les champs")';
print '</script>';
} else {
$from = "From: $name<$email>\r\nReturn-path: $email";
$subject = "Message sent using your contact form";
mail(desiletsmathieu#gmail.com", $subject, $message, $from);
print '<script type="text/javascript">';
print 'alert("Mail envoyé")';
print '</script>';
}
}
}
?>
This is happening because you are using $_REQUEST['action']
After your are submitting the form, your hidden field action becomes $_REQUEST['action'];
And after you submit the form, you get $action = $_REQUEST['action']; to be submit.
Where as you should have a blank value for $_REQUEST['action'] to display the form.
Solution:
1) Either, modify if ($action == "submit")
2) Or, assign blank value to the action (hidden) field
Try it this way.
First off, you forgot a double quote just after mail( that read like this:
mail(desiletsmathieu#gmail.com", $subject, $message, $from);
and needed to be changed to:
mail("desiletsmathieu#gmail.com", $subject, $message, $from);
Plus enctype="multipart/form-data" is for file attachments/uploading so you don't need that.
I also removed this line, which was no longer required:
<input type="hidden" name="action" value="submit">
This works and tested:
Note: I added a name to your submit button in order to give it an extra condition. Plus, you basically had your conditions already set, it just needed to be reworked/rethinked and using less code to achieve the same result.
<form action="" method="POST">
<input class="champTextFormulaire" placeholder="Votre Nom" name="name" type="text" value="" size="30"/><br>
<input class="champTextFormulaire" placeholder="Votre email" name="email" type="text" value="" size="30"/><br>
<textarea id="champMessage" placeholder="Votre Message..." name="message" rows="7" cols="30"></textarea><br>
<input class="btnEnvoiFormulaire" type="submit" name="submit" value="Envoi"/>
</form>
<?php
$name=$_REQUEST['name'];
$email=$_REQUEST['email'];
$message=$_REQUEST['message'];
if (isset($_POST['submit'])) {
if (($name=="")||($email=="")||($message==""))
{
print '<script type="text/javascript">';
print 'alert("Veuillez remplir tout les champs")';
print '</script>';
exit;
}
else
{
$from="From: $name<$email>\r\nReturn-path: $email";
$subject="Message sent using your contact form";
mail("desiletsmathieu#gmail.com", $subject, $message, $from);
print '<script type="text/javascript">';
print 'alert("Mail envoyé")';
print '</script>';
exit;
}
}
?>
Related
So the deal is this: the validation process starts after the user hits submit. However for some reason the code is not working.
PHP:
if ($_POST['submitted']) {
if(!filter_var($email, FILTER_VALIDATE_EMAIL))
my_contact_form_generate_response("error", $email_invalid);
else //email is valid
{
//validate presence of name and message
if (empty($name) || empty($message)) {
my_contact_form_generate_response("error", $missing_content);
} else //ready to go!
{
$sent = wp_mail($to, $subject, strip_tags($message), $headers);
if ($sent) my_contact_form_generate_response("success", $message_sent); //message sent!
else my_contact_form_generate_response("error", $message_unsent); //message wasn't sent
}
}
} else {
$response = "";
}
HTML:
<form action="<?php the_permalink(); ?>" class="contact" method="post">
<input type="text" placeholder="Your Name" name="message_name" value="<?php echo esc_attr($_POST['message_name']); ?>">
<input type="text" placeholder="Your Email" name="message_email" value="<?php echo esc_attr($_POST['message_email']); ?>">
<input type="text" placeholder="Your Company" name="message_company" value="<?php echo esc_attr($_POST['message_company']); ?>">
<textarea name="message_text" rows="10" placeholder="Your Message"><?php echo esc_textarea($_POST['message_text']); ?></textarea>
<button type="submit" class="contact-button" name="submitted">Submit</button>
</form>
<?php echo $response ?>
EDIT:
Sorry I forgot to add the last part of my post, what I ment was the PHP script should work the moment I hit the submit buttom. But the if ($_POST['submitted']) {} doesn't get triggered by my submit button. I was wondering why that is?
To me, it looks like you aren't assigning your variables to POST elements.
If you replace $email with $_POST['message_email'], $name with $_POST['message_name'], $message with $_POST['message_text'] in your first snippet you may find better luck.
Regardless, I can see quite a few unused variables in this snippet including $headers, $to, $subject, and every reporting variable used under my_contact_form_generate_*. Make sure these are also set.
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'];
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.
I´m new to PHP and I can't figure this out. Below you will see my PHP email form. The problem is that if the user forgets to enter an email and have been written a long message and then hit submit. The page will then reload and a text message comes up and says: You forgot your email. But now the whole message has been deleted from the form and they have to write it all over again.
How can I echo out that they forgot email or message without reload the page/delete already entered info.
<?php
function email() {
$email = $_REQUEST['email'];
$message = $_REQUEST['message'];
if ($email)
if ($message)
{
//send email
$to = "xxx#example.com";
$subject = "xxx" ;
$from = $_REQUEST['email'] ;
$message = $_REQUEST['message'] ;
$headers = "From:" . $from;
mail($to,$subject,$message,$headers);
header('Location: sent/');
exit();
}
else {
echo "You forgot your message.";
}
else {
echo "You forgot your email.";
}
}
?>
<form class="mail_form" accept-charset="ISO-8859-1" method='POST'>
<input class="input_mail" name='email' type='text' placeholder="Your email">
<textarea id="textarea_mail" accept-charset="ISO-8859-1" name='message' cols="30" rows="5" placeholder="Your message"></textarea>
<input class="send_mail" type='submit' name='submit'>
</form>
<?php if(isset($_POST['submit']))
{
echo email();
}
?>
You need to add related $_POST value to the field. if it has any it will display the message:
<form class="mail_form" accept-charset="ISO-8859-1" method='POST'>
<input class="input_mail" name='email' type='text' placeholder="Your email" value="<?php echo #$_POST['email']; ?>">
<textarea id="textarea_mail" accept-charset="ISO-8859-1" name='message' cols="30" rows="5" placeholder="Your message">
<?php echo #$_POST['message']; ?>
</textarea>
<input class="send_mail" type='submit' name='submit'>
</form>
<?php
if(isset($_POST['submit']))
{
echo email();
}
?>
just echo back the message like:
<textarea id="textarea_mail" accept-charset="ISO-8859-1" name='message'
cols="30" rows="5" placeholder="Your message">
<?php echo $_REQUEST['message']; ?>
</textarea>
but if you get involved a bit of java script or jQuery, you can validate the form before it gets submitted.
Use POST to bring everything to the next page. There you use hidden fields and save all availible values in them:
<input type="hidden" value="<?php echo $_POST['value']; ?>" />
When something is missing, use a submit-button to bring the user back to the last page, and again with POST you bring everything back to the last page.
Call your $message
<textarea id="textarea_mail" accept-charset="ISO-8859-1" name='message'
cols="30" rows="5" placeholder="
<?php if(isset($message)) { echo $message; } else { echo "Your message";} ?>" >
</textarea>
Don't forget
unset($message);
function email() {
$email = $_REQUEST['email'];
$message = $_REQUEST['message'];
if ($email)
if ($message)
{
//send email
$to = "xxx#example.com";
$subject = "xxx" ;
$from = $_REQUEST['email'] ;
$message = $_REQUEST['message'] ;
$headers = "From:" . $from;
mail($to,$subject,$message,$headers);
header('Location: sent/');
exit();
unset($message);
unset($mail);
}
else {
echo "You forgot your message.";
}
else {
echo "You forgot your email.";
}
}
?>
When the mail is send
I have PHP included a PHP form in my portfolio, that you can see here http://www.tinybigstudio.com The thing is that after SUBMIT, the page goes to TOP instead of staying at the bottom where the form is.
This is the PHP code I have:
<?php
//If the form is submitted
if(isset($_POST['submit'])) {
//Check to make sure that the name field is not empty
if(trim($_POST['contactname']) == '') {
$hasError = true;
} else {
$name = trim($_POST['contactname']);
}
//Check to make sure sure that a valid email address is submitted
if(trim($_POST['email']) == '') {
$hasError = true;
} else if (!eregi("^[A-Z0-9._%-]+#[A-Z0-9._%-]+\.[A-Z]{2,4}$", trim($_POST['email']))) {
$hasError = true;
} else {
$email = trim($_POST['email']);
}
//Check to make sure comments were entered
if(trim($_POST['message']) == '') {
$hasError = true;
} else {
if(function_exists('stripslashes')) {
$comments = stripslashes(trim($_POST['message']));
} else {
$comments = trim($_POST['message']);
}
}
//If there is no error, send the email
if(!isset($hasError)) {
$emailTo = 'info#tinybigstudio.com'; //Put your own email address here
$body = "Name: $name \n\nEmail: $email \n\nComments:\n $comments";
$headers = 'From: My Site <'.$emailTo.'>' .$subject = "You have mail, yes!" . "\r\n" . 'Te lo manda: ' . $name;
mail($emailTo, $subject, $body, $headers);
$emailSent = true;
}
}
?>
And this is the form:
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" id="contactform">
<fieldset>
<label for="name">Name</label>
<input type="text" size="50" name="contactname" id="contactname" value="" class="required">
<label for="email">Email</label>
<input type="text" size="50" name="email" id="email" value="" class="required">
<label for="message">Message</label>
<textarea rows="5" cols="50" name="message" id="message" class="required"></textarea>
<input type="submit" value="S E N D" class="send" name="submit" onmouseover="this.className='send_hover';"
onmouseout="this.className='send';">
</form>
<?php if(isset($hasError)) { //If errors are found ?>
<p class="error">Please check if you've filled all the fields with valid information. Thank you.</p>
<?php } ?>
<?php if(isset($emailSent) && $emailSent == true) { //If email is sent ?>
<p><strong>Message Successfully Sent!</strong></p>
<p>Thank you <strong><?php echo $name;?></strong> for sending a message!</p>
<?php }
?>
you need to add an anchor to the contact form and use this as the form action.eg:
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>#contact" >
and add this 'pseudo-link' right before the contact form
<a name="contact">Contact me</a>
Now after submit user will be send to the form directly and not the top of the page
This is because you are submitting it to the same page that you are on, therefore the browser goes effectively refreshes with the $_POST data. If you want to stop this you need to use AJAX to submit it so that it happens in the background. Take a look at the API - http://api.jquery.com/jQuery.ajax/