Solution 1 :

Your condition is backwards.

The code will be simpler if you build $txt incrementally with concatenation.

$txt = "Message received.nn";
if ($specific1 != "" && $specific2 != "") {
    $txt .= "Specific-text-1: $specific1nSpecific-text-2: $specific2nn";
}
$txt .= "Message context:nn$message";

The .= operator appends the string to the variable.

This will add the specific text fields to the mail only if both of them are filled in by the user.

Problem :

This is my first site with PHP and I’m making contact form that sends mail to me using PHP.
I want to set two different messages:

  1. First message sends if all fields are filled;
  2. Second message sends if one of two specific inputs (specific-text-1 and specific-text-2) are empty.

HTML in contact.html

<form id="query-form" class="query-form" method="post">
  <input type="text" name="specific-text-1" placeholder="Text 1">
  <br>
  <input type="text" name="specific-text-2" placeholder="Text 2">
  <br>
  <input type="text" name="subject" placeholder="Email Subject">
  <br>
  <textarea name="message" placeholder="Message text" maxlength="700"></textarea>
  <br>
  <input type="submit" name="submit" placeholder="Send Message">
</form>

PHP in other file, contact.php

<?php
  if (isset($_POST['submit'])) {
  $subject = $_POST['subject'];
  $message = $_POST['message'];
  // I want $specific1 and $specific2 to be in if command
  $specific1 = $_POST['specific-text-1'];
  $specific2 = $_POST['specific-text-2'];
  $mailTo = "[email protected]";

  $txt = "Message received.nnSpecific-text-1: ".$specific1."n"."Specific-text-2: ".$specific2."nn"."Message context: nn".$message;
  mail($mailTo, $subject, $txt);
  }
?>

instead of $txt = "Message received...", I want to use something like this:

if (specific-text-1 == '' || specific-text-2 == '') {
  $txt = "Message2 received.nn"."Message context: nn".$message;
}
else {
  $txt = "Message received.nnSpecific-text-1: ".$specific1."n"."Specific-text-2: ".$specific2."nn"."Message context: nn".$message;
}

How do I get PHP to know if specific-text-1 or specific-text-2 texts that user types are empty and send Message2 or if are both full send Message?

Thank you all very much!

Comments

Comment posted by Quazey

can I do

Comment posted by Barmar

Yes, you could do that instead, if you only want to include them if both are filled in.

Comment posted by Barmar

Of course it looks for user input on those elements, that’s what

Comment posted by Quazey

I tried with what you anwered me, with

Comment posted by Barmar

You’re saying that you got the message with

By