-1

I'm trying everything but this PHP script show this error "Please fill out all the mandatory fields."

Please help me with how to solve this problem.

<?php
session_start();
if (isset($_POST['fullname']) &&  isset($_POST['email']) && isset($_POST['phone'])) {
    $fullname = $_POST['fullname'];
    $email = $_POST['email'];
    $phone = $_POST['phone'];
    $headers = "MIME-Version: 1.0"."\r\n";
    $headers.= "Content-type:text/html;charset=UTF-8"."\r\n";
  $headers.= 'From: <'.$email.'>'."\r\n";
  $mailto = "google@gmail.com";
  $subject = "Web Design & Development Service";
  $msg2send = "Hi $fullname,
  Hi, we have received one fresh query for you.
  Name: $fullname
  Email: $email
  Phone: $phone ";
  $msg2send = nl2br($msg2send);
  if (mail($mailto, $subject, $msg2send, $headers)) {
      echo "Thanks for writing to us. We will get back to you as soon as possible.";
  } else {
      echo "Please fill out all the mandatory fields.";
  }
} else {
    echo "Your enquiry could not be sent for some reason; please try sending us again.";
}
?>
Parveen
  • 1
  • 4
  • 1
    Your `else` conditions are flipped - the one saying the mail failed to be sent should be after `if(mail(..))`, the other else should be against the `if (isset..)` conditions. – Qirel Mar 20 '19 at 06:13
  • That will not filx that mail was not sent though, see the duplicate. – Qirel Mar 20 '19 at 06:14
  • FYI, You can condense your three isset calls at the top of your script into a single isset call with 3 parameters. – mickmackusa Mar 25 '19 at 09:59

2 Answers2

1

Your if else statements are positioned incorrectly.

They point to wrong conditions.

The rearranged code:

<?php
session_start();
if (isset($_POST['fullname']) &&  isset($_POST['email']) && isset($_POST['phone'])) {
    $fullname = $_POST['fullname'];
    $email = $_POST['email'];
    $phone = $_POST['phone'];
    $headers = "MIME-Version: 1.0"."\r\n";
    $headers.= "Content-type:text/html;charset=UTF-8"."\r\n";
  $headers.= 'From: <'.$email.'>'."\r\n";
  $mailto = "google@gmail.com";
  $subject = "Web Design & Development Service";
  $msg2send = "Hi $fullname,
  Hi, we have received one fresh query for you.
  Name: $fullname
  Email: $email
  Phone: $phone ";
  $msg2send = nl2br($msg2send);
  if (mail($email, $subject, $msg2send, $headers)) {
      echo "Thanks for writing to us. We will get back to you as soon as possible.";
  } else {
    // #1: Swipe with #2
      echo "Your enquiry could not be sent for some reason; please try sending us again."; // Flip this with #2
  }
} else {
  echo "Please fill out all the mandatory fields."; // #2
}

?>

Pupil
  • 23,834
  • 6
  • 44
  • 66
  • right now show this "Your enquiry could not be sent for some reason; please try sending us again." – Parveen Mar 20 '19 at 06:21
  • `$mailto` should be replaced by `$email`, the actual email address. Please see the updated answer. – Pupil Mar 20 '19 at 06:23
  • not working please send me 2nd script – Parveen Mar 20 '19 at 06:27
  • @Parveen "not working" isn't an error message or problem statement that anyone can fix. It's too vague. Please give some more specific information. And also please do some simple debugging of your code so you can see what paths your code takes and what values the variables are getting. That way you might start to see which part is going wrong – ADyson Mar 20 '19 at 07:26
1

Too many nested IF statements is a bad practice because it is difficult to debug. Instead, you should break them to small statements

For example:

<?php
session_start();
if !(isset($_POST['fullname']) ||  isset($_POST['email']) || isset($_POST['phone'])) {
    echo "Your enquiry could not be sent for some reason; please try sending us again.";
    exit();
}

$fullname = $_POST['fullname'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$headers = "MIME-Version: 1.0"."\r\n";
$headers.= "Content-type:text/html;charset=UTF-8"."\r\n";
$headers.= 'From: <'.$email.'>'."\r\n";
$mailto = "google@gmail.com";
$subject = "Web Design & Development Service";
$msg2send = "Hi $fullname,
Hi, we have received one fresh query for you.
Name: $fullname
Email: $email
Phone: $phone ";
$msg2send = nl2br($msg2send);

if (mail($mailto, $subject, $msg2send, $headers)) {
  echo "Thanks for writing to us. We will get back to you as soon as possible.";
} else {
  echo "Please fill out all the mandatory fields.";
}
?>

Now the code becomes clearer and you can trace back. Seem your code has error at mail function so it return False, then the message shows up.

Duc Vu
  • 95
  • 1
  • 1
  • 8
  • this IF statements are not working – Parveen Mar 20 '19 at 07:14
  • It could be anywhere, could be the mail function, the mail server has not configured correctly. You need to trace where the code stops. For example, place a command like echo('1'), echo('2') in each IF statement, then adjust the position of the command until you dont see echo on the screen. Then you may find out where your code stops. – Duc Vu Mar 20 '19 at 07:35
  • i'm place a comment **echo $msg2send = nl2br($msg2send);** then show this Hi Parveen HS, Hi, we have received one fresh query for you. Name: Parveen HS Email: *********@gmail.com Phone: ********** Please fill out all the mandatory fields. – Parveen Mar 20 '19 at 07:49
  • @DucVu you forgot to change the echo statements around...it doesn't make any meaningful sense. Swap the line `echo "Your enquiry could not be sent for some reason; please try sending us again.";` with the line `echo "Please fill out all the mandatory fields.";` and the code will output the correct error message in each situation. – ADyson Mar 20 '19 at 09:40
  • @Parveen bearing in mind my comment just above this one, what you're seeing actually means the `mail()` function failed and could not send the information to the mailserver. Now, the duplicate question linked in the main comments thread above gives you clues about how to debug that. It might, or might not, be the fault of your code. Most likely not, unless you are setting a bad value in one of the headers or something. – ADyson Mar 20 '19 at 09:50
  • @Parveen ...cont'd. More likely it's something to do with the process - maybe the mailserver isn't configured, or perhaps it's a rule like you aren't allowed to send a mail on behalf of a random user (i.e. whoever is in the "from" field) - spoofing the sender like that is usually viewed as an attempt to create spam. Better to just use a generic "donotreply@yourdomain.com" address in the from field. You are already passing the input email address in the message body, so there's no need to put it in the "from" field as well. At a guess I would try that as a first step. – ADyson Mar 20 '19 at 09:52