Don't send message if length < 2 - php

I am trying to add php to my contact us page to make sure the message is at least a few characters long for it to be sent. I have tried if (strlen()> 2) but it is just letting messages go through anyway.enter code here
<div id="phillya11">
<a href="#" style="text-decoration:none;color:#FFF5ee">
Contact us
</a>
</div>
<div id="soon4">
<form action="index.php" method="POST">
<p>Name</p> <input type="text" name="name">
<p>Email</p> <input type="text" name="email">
<p>Message</p>
<textarea style="width:475px; height:175px;margin-left:7%" name="message"></textarea><br />
<input type="submit" value="Send"><input type="reset" value="Clear">
</form>
</div>
PHP:
<?php
if (strlen($message)> 2)
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$formcontent="From: $name \n Message: $message";
$recipient = "myemail";
$subject = "Contact Form";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
echo "Thank You!";
?>
jQuery:
$("#phillya11").click(function ()
{
$("#soon4").show();
$("#soon").hide();
$("#soon1").hide();
$("#soon2").hide();
$("#soon3").hide();
$('#close4').click(function ()
{
$("#soon4").hide();
});

Your $message is not set yet so the if statement will fail. Change it to the post message. You also need to check the message is set before you check its string length.
Note also the use of { } with the if statement when the statement is true the content between the curly braces executes.
<?php
//$message is not set here until inside the if statment
//there for we should check $_POST['message']
//if $_POST['message'] has the criteria you seek
//THEN $message will be set as shown at line 5 which i marked for you to see
if (isset($_POST['message']) && strlen($_POST['message'])> 2) {
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message']; //line 5 $message now set
$formcontent="From: $name \n Message: $message";
$recipient = "myemail";
$subject = "Contact Form";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
echo "Thank You!";
} else {
echo 'Your message must be longer than 2 characters!';
}
?>

This method uses the if message is less than 2 characters, then do not process the execution for the mail() function. If the message is more than that, then it will proceed in executing the mail() function.
<?php
if (strlen($_POST['message']) < 2) {
die("Sorry, try again.");
}
else {
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$formcontent="From: $name \n Message: $message";
$recipient = "myemail";
$subject = "Contact Form";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
echo "Thank You!";
}
?>

<?php
$message = $_POST['message'];
if (strlen($message)> 2) {
$name = $_POST['name'];
$email = $_POST['email'];
$formcontent="From: $name \n Message: $message";
$recipient = "myemail";
$subject = "Contact Form";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
echo "Thank You!";
}
?>

Try this
if (strlen($_POST['$message'])> 2) {
' do something;
}

Related

PHP Contact Form Blank Data & Unknown Sender

Hello I have made a contact form on my website but even if the user doesn't type in any of the details and presses the submit button it sends me a blank email with nothing in it from Unknown Sender.
Anyone know why this is happening? I have added form validation so it shouldn't be sending anything.
HTML Code:
<form action="mail.php" method="POST">
<font color="red">*</font> Name <input type="text" name="name" required>
<font color="red">*</font> Phone <input type="text" name="phone" required>
<font color="red">*</font> Email <input type="text" name="email" required>
<font color="red">*</font> Message <input type="text" name="message" placeholder="I am looking for..." required><br />
<input type="image" src="images/Landing_Pages/submit.png" border="0" alt="Submit" />
</form>
PHP Code:
<?php $name = $_POST['name'];
$phone = $_POST['phone'];
$preferred = $_POST['preferred'];
$email = $_POST['email'];
$formcontent="From: $name \n Phone: $phone \n Email: $email \n Message: $message \n Preferred Contact: $preferred \n Email: $email";
$recipient = "bwebb#webbmaster.com.au";
$subject = "New Request Southbank";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
?>
You have to check if post data is not empty
if (!empty($name) && !empty($phone) && !empty($preferred) && !empty($email)) {
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
}
Edit
<?php
$name = $_POST['name'];
$phone = $_POST['phone'];
$message = $_POST['message'];
$preferred = $_POST['preferred'];
$email = $_POST['email'];
$formcontent="From: $name \n Phone: $phone \n Email: $email \n Message: $message \n Preferred Contact: $preferred \n Email: $email";
$recipient = "bwebb#webbmaster.com.au";
$subject = "New Request Southbank";
$mailheader = "From: $email \r\n";
if (!empty($name) && !empty($phone) && !empty($message) && !empty($preferred) && !empty($email)) {
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
}
?>
Do something like :
$errorflag = 0 ;
if(empty($name)){$errorflag = 1 ;$error = "Input your Name plz!"; };
if(empty($phone)){$errorflag = 1 ;$error = "Input phone"; }
...
if(!$errorflag) {
#mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
}
check every input you need and then send

Thank you $name is displaying as it is not showing the persons name

<?php
$Email = #trim(stripslashes($_POST['Email']));
$Name = $_POST['Name'];
$Email = $_POST['Email'];
$Number= $_POST['Number'];
$Subject= $_POST['Subject'];
$Message= $_POST['Message'];
$formcontent="
Name: $Name \n
Email: $Email \n
Number: $Number \n
Subject: $Subject\n
Message: $Message";
$recipient = "info#domain.com";
$mailheader = "From: $Name \r\n";
mail($recipient, $Subject, $formcontent, $mailheader) or die("Error!");
echo "<h4 align= center><b>Thank You $Name!</b></h4>";
?>
For this PHP code the result displaying as Thank You $Name!";?> in offline not in serverside. Why it is not showing the person name?
Try this :\
echo "<h4 align= center><b>Thank You ".$Name."!</b></h4>";
Try like this,
echo "<h4 align= center><b>Thank You '".$Name."'!</b></h4>";

Contact Form that shows new page then reverts back to home page?

I have a basic contact form on my site that works fine. When submitted though it opens a blank page with "Thank you" ... I would like to potentially open a styled web page that says "submitted successfully" displays for around 3 seconds, then reverts back to the main page. I'm quite new to web design and especially PHP so if anyone could give any tips that would be great. I have put the PHP code below:
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$formcontent = "From: $name \r\n Message: $message";
$recipient = "myemail#email.com";
$mailheader = "From: $email \r\n";
mail($recipient, $message, $formcontent, $mailheader) or die("Error!");
echo "Thank You!";
?>
Try:
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$formcontent = "From: $name \r\n Message: $message";
$recipient = "myemail#email.com";
$mailheader = "From: $email \r\n";
mail($recipient, $message, $formcontent, $mailheader) or die("Error!");
?><!DOCTYPE html>
<html>
<head>
<meta http-equiv="refresh" content="3; url=http://mysite.com/contact.php">
</head>
<body>
<p>Submitted successfully</p>
</body>
</html>
If the email is sent, the message "Submitted successfully" will show. Three seconds later, it will redirect to http://mysite.com/contact.php. Change http://mysite.com/contact.php to the URL you want to return to.
Although Wayne Whitty's answer is right, I do have another note/answer:
To redirect after 3 seconds use this:
<meta http-equiv="refresh" content="3;url=http://YOURPAGE.php">
I see you are redirecting from a different page but that is pretty useless. You could also just put this in the main page and use this to only redirect after submitting. (The form is an example, I dont know how your form looks like)
<form action="register.php" method="post">
Username <input type="text" name="name">
Password <input type="text" name="email">
Message <input type="text" name="message">
<input name="register" type="submit" value="Register">
</form>
<?php
if (isset($_POST['name'])) {
echo'<meta http-equiv="refresh" content="3;url=http://YOURPAGE.php">';
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$formcontent = "From: $name \r\n Message: $message";
$recipient = "myemail#email.com";
$mailheader = "From: $email \r\n";
mail($recipient, $message, $formcontent, $mailheader) or die("Error!");
echo "submitted successfully";
}
?>
Also just replace: echo "Thank You!"; with echo "submitted successfully";

How do I redirect to an HTML page after submitting a PHP email form?

Here is my code:
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$formcontent = "From: $name \n Email: $email \n Subject: $subject \n Message: $message";
$recipient = "hunter#dreaminginhd.com";
$subject = "Contact Form";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
?>
I just need to know how to redirect to an HTML page after clicking submit and to make sure that the script with send the email. Any help will be greatly appreciated!
Add this to the end of the script:
header("Location: URL");
where URL is the URL of the page you want to redirect to.
php
header('Location: login.php?msg=1');
javascript
<script> window.location='forgot.php'</script>";
html
<META HTTP-EQUIV="REFRESH" CONTENT="3;URL=http://google.com">
You are possibly missing the open form and close form or you just include it
<form name="PL" action="newForm.php" method="post">
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$formcontent = "From: $name \n Email: $email \n Subject: $subject \n Message:
$message";
$recipient = "hunter#dreaminginhd.com";
$subject = "Contact Form";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
include "newForm2.php";
?>
<input type="submit" name="btn_submit" value="Display Invoice" />
</form>
First we get the mailto function to work with localhost and email client:
Check this link on stackoverflow:
URL link: send email to owner by collecting information provided by user through form?
Then I recommend using Swiftmailer. http://swiftmailer.org/docs/sending.html They got the best manual.

php form won't send or go to header location after submit

I have a simple php form for contact purposes. However, it won't send the email or go to the correct page after submit. It redirects to the mail.php instead.
My contact form named contact.php is as follows:
<form id="contact-form" action="mail.php" method="POST">
<fieldset>
<label><span class="text-form">Your Name:</span> <input type="text" name="name"></label>
<label><span class="text-form">Your Email:</span><input type="text" name="email"></label>
<label><span class="text-form">Your Contact No:</span><input type="text" name="contact"></label>
<div class="wrapper">
<div class="text-form">Your Message:</div>
<textarea name="message" rows="6" cols="25"></textarea><br />
<div class="clear">
</div>
<div class="buttons">
<a class="button" href="#"><input class="button" type="submit" value="Send"></a>
<a class="button" href="#"><input class="button" type="reset" value="Clear"></a>
</div>
</fieldset>
</form>
And the php code named mail.php is as follows:
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
&contact = $_POST['contact'];
$formcontent="From: $name \n Message: $message";
$recipient = "info#whatever.co.za";
$subject = "Contact form message";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
header("Location:contact.php");
?>
What am i doing wrong. It just wont send the message to email or redirect to the correct page??
Your syntax is ever so slightly wrong. &contact on line 5 should be $contact. I assume you're on a production server, so error reporting would be disabled and you wouldn't get any warnings.
Try using # before mail function
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$contact = $_POST['contact'];
$formcontent="From: $name \n Message: $message";
$recipient = "info#whatever.co.za";
$subject = "Contact form message";
$mailheader = "From: $email \r\n";
#mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
header("location: contact.php");
?>
this will let you pass even if mail function produce any issue. Also please check if there are any other header are not sent using http://php.net/manual/en/function.headers-sent.php function.
I would strongly suggest to use PHPMailer or some other class to send any kind of email.
Please check the contact.php file whether some redirection is happening from contact.php to mail.php
I think there may be an issue sending mail from your server. The mail.php file is probably tripping up on the mail function and not doing the redirect.
Create a new file with just the php mail function in there (with your email address and a test message) and browse it from your web browser, does it send?
Try to use ob_start() at the start of script, and exit(); after header("Location: contact.php");
smth like this ...
<?php
ob_start();
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
&contact = $_POST['contact'];
$formcontent="From: $name \n Message: $message";
$recipient = "info#whatever.co.za";
$subject = "Contact form message";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
header("Location: contact.php");
exit();
?>
Change the mail.php to the following:
<?php
mail("info#whatever.co.za", "Contact form message", $_POST['message'], "From: ".$_POST['name']." <".$_POST['email'].">");
header("Location: contact.php");
?>

Categories