-3
<?php 
session_start();

if (!$_SESSION['loggedInUser']) {
 header("Location: login.php");
}
$resultMessage = null;
include ('db.php');
$query = "SELECT * from user";
$result = mysqli_query($connection, $query);
mysqli_close($connection);
 ?>

<!DOCTYPE html>
<html lang="en">

 <head>
 <meta charset="utf-8">
 <meta http-equiv="X-UA-Compatible" content="IE=edge">
 <meta name="viewport" content="width=device-width, initial-scale=1">
 <title>Client Area</title>

 </head>

 <body>

 <?php 
 // Check for Header Injections
 function has_header_injection($str) {
 return preg_match( "/[\r\n]/", $str );
 }

 $missingitemname = '<p><strong> Please Enter Item Name</p></strong>';

 if (isset($_POST['submit_form'])) {

 $itemname = $_POST["item-name"];
 $brandname = $_POST["brand"];
 $description = $_POST["description"];
 $availability = $_POST["availability"];
 $contact = $_POST["contact-p"];
 $address = $_POST["address"];
 $email = $_POST["email"];
 $location = $_POST["location"];
 $file = $_FILES["attach"];
 if (!$itemname) {
 $errors .= $missingitemname;
 } else {
 $itemname = filter_var($itemname, FILTER_SANITIZE_STRING);
 }

 if (!$email) {
 $errors .= $missingemail;

 } else {
 $email = filter_var($email, FILTER_SANITIZE_EMAIL);
 if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
 $errors .= $invalidEmail;
 }

 }
 if (!$brandname) {
 $errors .= $missingbrandname;
 } else {
 $brandname = filter_var($brandname, FILTER_SANITIZE_STRING);

 }



 if ($errors) {
 $resultMessage = '<div class="alert alert-danger">'. $errors .'</div>';
}
 else {
 $to = "adamskhan123@gmail.com";
 $subject ="New Contact";
 $message ="<p>Item Name: $itemname.</p>
 <p>Brand: $brandname.</p>
 <p>Description: $description.</p>
 <p>Availability: $availability.</p>
 <p>Contact Person: $contact</p>
 <p>Email: $email</p>
 <p>Location: $location</p>
 ";
 $headers ="Content-type: text/html";
 if (mail($to, $subject, $message, $headers)) {
 $resultMessage = '<div class="alert alert-success">Thank You. Your message has been sent</div>';
 } else {
 $resultMessage = '<div class="alert alert-danger">Sorry Please Try Again.</div>';
 }
 }

}

?>

 <div class="container">
 <h1>PinTribe</h1>
 <p>Welcome <?php echo $_SESSION['loggedInUser']; ?></p>
</div>
<div class="top-content">

 <div class="inner-bg">
 <div class="container">
 <div class="row">
 <div class="col-sm-8 col-sm-offset-2 text">

 </div>
 </div>
 <div class="row">
 <div class="col-sm-6 col-sm-offset-3 form-box">
 <div class="form-top">
 <div class="form-top-left">


 </div>

 </div>

 <div class="form-bottom">
 <?php echo $resultMessage; ?>
 <form role="form" action="dashboard.php" method="post" class="login-form" enctype="multipart/form-data">
 <div class="form-group">
 <label class="sr-only" for="item-name">Item Name</label>
 <input type="text" name="item-name" placeholder="Item Name" class="username form-control" id="item-name">
 </div>

 <div class="form-group">

 <textarea type="text" name="description" placeholder="Description" class="description form-control" id="description" rows="5"></textarea>
 </div>

 <div class="form-group">
 <label class="sr-only" for="email">Email</label>
 <input type="email" name="email" placeholder="Email.." class="email form-control" id="email">
 </div>


 </div>

This is my code the problem i am getting is when I submit my form I get Sorry Please Try Again. which is failure message I added. I rechecked my code but I am not sure why I am getting this error. I filled every field but still i am not sure why it is happening. can anyone of figure it out?

Abhishek Gurjar
  • 7,426
  • 10
  • 37
  • 45
Ali
  • 1
  • 2
  • 7
  • Have you checked your server's error logs? – Jay Blanchard Jun 03 '16 at 17:18
  • 6
    Waaayyyy too much code here. You need to do *more* debugging and narrow it down a specific portion of code. As of right now it just looks like you wrote a bunch of code, didn't test it, and now want us to fix it for you. – John Conde Jun 03 '16 at 17:18
  • 1
    Why do you think the issue is with submitting or not filled out fields? The issue is with your `mail()` function. That's where it decides whether to show the "sorry" message or not. – gen_Eric Jun 03 '16 at 17:20
  • Amen @JohnConde :D – Boltz0r Jun 03 '16 at 17:21
  • You have no `submit_form` field in your form, or you forgot to paste in that part fo the code. if it's NOT in the form, then you never process anything, because the field you check to start processing doesn't exist. – Marc B Jun 03 '16 at 17:44

3 Answers3

2

That's because the call to mail() in if (mail($to, $subject, $message, $headers)) { returns false.

As to why it is failing: PHP mail form doesn't complete sending e-mail has the details on how to debug why mail() is not sending email.

Community
  • 1
  • 1
  • Hi it is not showing any error :/ – Ali Jun 03 '16 at 17:32
  • Check http://stackoverflow.com/questions/24644436/php-mail-form-doesnt-complete-sending-e-mail for debugging why mail() does not send email. –  Jun 03 '16 at 22:31
1

Please, refer on the PHP manual for the mail() function http://php.net/manual/en/function.mail.php

As you can see, it returns a "bool" value, in your case, it return FALSE.

For a quick test of mail() comment this part of your code

 $to = "adamskhan123@gmail.com";
 $subject ="New Contact";
 $message ="<p>Item Name: $itemname.</p>
 <p>Brand: $brandname.</p>
 <p>Description: $description.</p>
 <p>Availability: $availability.</p>
 <p>Contact Person: $contact</p>
 <p>Email: $email</p>
 <p>Location: $location</p>
 ";
 $headers ="Content-type: text/html";
 if (mail($to, $subject, $message, $headers)) {
 $resultMessage = '<div class="alert alert-success">Thank You. Your message has been sent</div>';
 } else {
 $resultMessage = '<div class="alert alert-danger">Sorry Please Try Again.</div>';
 }
 }

Add a simple line like:

mail('adamskhan123@gmail.com','My Subject','Testing');

You also may use var_dump to see what is the current value.

Hope it helps.

Marc Giroux
  • 186
  • 1
  • 7
  • 18
0

This line is false: if (mail($to, $subject, $message, $headers)) { delete mail and the clinches. Try on the variables other conditions like isset($var) not the whole function.

FileX
  • 11
  • 4