So I have a form:
<form method="post" action="contactus.php?message=ok" name="myForm" autocomplete="off">
<label for="Name">Name:</label>
<input type="text" name="Name" id="Name" maxlength="60" required/>
<label for="email">Email:</label>
<input type="text" name="email" id="email" maxlength="120" required/>
<label for="message">Message:</label><br />
<textarea name="message" rows="20" cols="20" id="message" required></textarea>
<input type="submit" name="submit" value="Submit" class="submit-button" onsubmit="displayMessage()" />
And the code to send the email:
<?php
if($_POST["submit"]) {
// The message
$message=$_POST["message"];
$email=$_POST["email"];
// In case any of our lines are larger than 70 characters, we should use wordwrap()
$message = wordwrap($message, 70, "\r\n");
// Send
mail('myemail.co.uk', $email, $message);
$sent_mail = true;
}
?>
And finally:
<?php
if (isset($sent_mail)) {
echo 'Thank you. We will be in touch soon.';
}
?>
So when the email is sent, sent_mail is set to 'true' and therefore the thank you message should be echoed. But right now this isn't working. The email sends first but the thank you message doesn't show. I basically just need a thank you message to come up somewhere on the page when the submit button is pressed.
Any ideas?
Instead of isset use simply if
Like this
<?php
if ($sent_mail) {
echo 'Thank you. We will be in touch soon.';
}
else
echo 'Unale to send message';
?>
mail function returns a boolean (true/false), so you can do like this
if (mail('myemail.co.uk', $email, $message)) {
echo "Thank you. We will be in touch soon.";
} else {
echo "Something went wrong, the email was not sent!";
}
Also, the structure of mail (the parameters) are to-address, subject, message. Which means that your current subject is the email-address, I'm not sure if this is what you intended?
Use
if(mail('myemail.co.uk', $email, $message))
$sent_mail = true;
else
$sent_mail = false;
And finally:
<?php
if ($sent_mail) {
echo 'Thank you. We will be in touch soon.';
}
else
echo 'Message cannot be send';
?>
You are assigning Boolean to $sent_mail and you set it to True.
<?php if($sent_mail){
echo "Email sent successfully";} ?>
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'];
how to set validation in contact from
suppose if user submit empty field then how to show Invalid input
now email address only invalid input show i want to fix all field validation please help me how can i do this
thanks in advance
<html>
<body>
<?php
function spamcheck($field) {
// Sanitize e-mail address
$field=filter_var($field, FILTER_SANITIZE_EMAIL);
// Validate e-mail address
if(filter_var($field, FILTER_VALIDATE_EMAIL)) {
return TRUE;
} else {
return FALSE;
}
}
?>
<h2>Form</h2>
<?php
// display form if user has not clicked submit
if (!isset($_POST["submit"])) {
?>
<form method="post" action="<?php echo $_SERVER["PHP_SELF"];?>">
From: <input type="text" name="email"><br>
Subject: <input type="text" name="subject"><br>
Message: <textarea rows="10" cols="40" name="message"></textarea><br>
<input type="submit" name="submit" value="Submit Feedback">
</form>
<?php
} else { // the user has submitted the form
// Check if the "from" input field is filled out
if (isset($_POST["email"])) {
// Check if "from" email address is valid
$mailcheck = spamcheck($_POST["email"]);
if ($mailcheck==FALSE) {
echo "Invalid input";
} else {
$email = $_POST["email"]; // sender
$subject = $_POST["subject"];
$message = $_POST["message"];
// message lines should not exceed 70 characters (PHP rule), so wrap it
$message = wordwrap($message, 70);
// send mail
mail("demo#gmail.com",$subject,$message,"From: $email\n");
echo "Thank you for sending us feedback";
}
}
}
?>
</body>
</html>
Use these
From: <input type="email" name="email" required><br>
Subject: <input type="text" name="subject" required><br>
Message: <textarea rows="10" cols="40" name="message" required></textarea><br>
Refer this and this
I have a really simple php contact form on one of my sites, the problem is that it won't work when sending emails to some addresses.
It works fine sending to my gmail address, but it doesn't work with iCloud (#me.com) addresses or other domain specific emails that I have set up.
<?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">
Your name:<br>
<input name="name" type="text" value="" size="30"/><br>
Your email:<br>
<input name="email" type="text" value="" size="30"/><br>
Your message:<br>
<textarea name="message" rows="7" cols="30"></textarea><br>
<input type="submit" value="Send email"/>
</form>
<?php
}
else /* send the submitted data */
{
$name=$_REQUEST['name'];
$email=$_REQUEST['email'];
$message=$_REQUEST['message'];
if (($name=="")||($email=="")||($message==""))
{
echo "All fields are required, please fill the form again.";
}
else{
$from="From: $name<$email>\r\nReturn-path: $email";
$subject="Message sent using your contact form";
mail("myemailaddress#me.com", $subject, $message, $from);
echo "Email sent!";
}
}
?>
If it didn't work at all I'd know there was a syntax error, but I get the 'Email Sent!' confirmation.
add if statement to the mail function :
if( mail("myemailaddress#me.com", $subject, $message, $from)) echo "Email sent!";
else echo "failed" ;
this way you'll know if it was sent or not .
then start checking the problem .
you man check your php.ini file :
check
sendmail_from = '';
sendmail_path = '';
and fill them with needed data ... maybe some address doesnt accept emails with no full data in the header . maybe they found it as a spam or somthing else .
Just have a look at your else part where you are using mail .Just below that is the echo so it always get executed as soon as it enters the else block.So, check for mail by if & else condition and then show the massage accordingly
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
}
}
?>