Reputation: 380
i have a this php script that send webmail:
<?php
session_start();
//error_reporting(0);
if(isset($_POST['submitted'])) {
$sendto = "[email protected]";
$usermail = strip_tags($_POST['email']);
$content = nl2br($_POST['message']);
$phone = strip_tags($_POST['phone']);
$name = strip_tags($_POST['name']);
$subject = "New Feedback Message";
$headers = "From: " . strip_tags($usermail) . "\r\n";
$headers .= "Reply-To: ". strip_tags($usermail) . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html;charset=utf-8 \r\n";
$msg = "<html><body style='font-family:Arial,sans-serif;'>";
$msg .= "<h2 style='font-weight:bold;border-bottom:1px dotted #ccc;'>New User Feedback</h2>\r\n";
$msg .= "<p><strong>Sent by:</strong> ".$usermail."</p>\r\n";
$msg .= "<p><strong>Message:</strong> ".$content."</p>\r\n";
$msg .= "<p><strong>Name:</strong> ".$name."</p>\r\n";
$msg .= "<p><strong>Phone:</strong> ".$phone."</p>\r\n";
$msg .= "</body></html>";
}
if(@mail($sendto, $subject, $msg, $headers)) {
$_SESSION['errormsg'] = "Mail Sent. Thank you we will contact you shortly.";
echo '<center><p style="color:green">' . $_SESSION['errormsg']. '</p></center>';
} else {
$_SESSION['errormsg'] = "Mail not Sent. Try Again.";
echo '<center><p style="color:red">' . $_SESSION['errormsg']. '</p></center>';
}
the above code works very fine but my problem is it show "Mail Sent. Thank you we will contact you when ever the page is loaded
here is my HTML code
<div class="col-md-6 contact-grid">
<form name="submitted"action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="POST">
<div class="styled-input wow slideInUp animated animated" data-wow-delay=".5s">
<input type="text" id="name" required />
<label>Name</label>
<span></span>
</div>
<div class="styled-input wow slideInUp animated animated" data-wow-delay=".5s">
<input type="email" id="email"required />
<label>Email</label>
<span></span>
</div>
<div class="styled-input wow slideInUp animated animated" data-wow-delay=".5s">
<input type="tel" id="phone"required />
<label>Phone</label>
<span></span>
</div>
<div class="styled-input wide wow slideInUp animated animated" data-wow-delay=".5s">
<textarea id="message" required></textarea>
<label>Message</label>
<span></span>
</div>
<div class="send wow shake animated animated" data-wow-delay=".5s">
<input name="submit" type="submit" value="Send" >
</div>
</form>
</div>
So my problem is how do i make it to display error message only when form is submitted.please Note every thing has to happen on a single page.
Upvotes: 0
Views: 54
Reputation: 94662
Your mail()
is outside the IF that checks that some data was sent to the form. Put it inside the if. Proper code indenting shows that in a second
<?php
session_start();
//error_reporting(0);
if(isset($_POST['submitted'])) {
$sendto = "[email protected]";
$usermail = strip_tags($_POST['email']);
$content = nl2br($_POST['message']);
$phone = strip_tags($_POST['phone']);
$name = strip_tags($_POST['name']);
$subject = "New Feedback Message";
$headers = "From: " . strip_tags($usermail) . "\r\n";
$headers .= "Reply-To: ". strip_tags($usermail) . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html;charset=utf-8 \r\n";
$msg = "<html><body style='font-family:Arial,sans-serif;'>";
$msg .= "<h2 style='font-weight:bold;border-bottom:1px dotted #ccc;'>New User Feedback</h2>\r\n";
$msg .= "<p><strong>Sent by:</strong> ".$usermail."</p>\r\n";
$msg .= "<p><strong>Message:</strong> ".$content."</p>\r\n";
$msg .= "<p><strong>Name:</strong> ".$name."</p>\r\n";
$msg .= "<p><strong>Phone:</strong> ".$phone."</p>\r\n";
$msg .= "</body></html>";
if(mail($sendto, $subject, $msg, $headers)) {
$_SESSION['errormsg'] = "Mail Sent. Thank you we will contact you shortly.";
echo '<center><p style="color:green">' . $_SESSION['errormsg']. '</p></center>';
} else {
$_SESSION['errormsg'] = "Mail not Sent. Try Again.";
echo '<center><p style="color:red">' . $_SESSION['errormsg']. '</p></center>';
}
}
Upvotes: 1