jeudi 20 février 2020

Obtaining PHP variable from HTML

Hello Stackoverflow community,

This is my first site with PHP and I'm making contact form on it that sends mail to me using PHP. I want to set two different messages: 1. If all fields are filled; 2. If one of two specific <input type="text"> are empty , show other message in my mail.

NOTE: If you're getting hard time reading full code, look for 'specific-text-1' and 'specific-text-2'

This is how my HTML [contact.html] looks like:

    <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>

In other file [contact.php], this is my code:

<?php echo file_get_contents("html/support.html"); 
  if (isset($_POST['submit'])) {
  $subject = $_POST['subject'];
  $message = $_POST['message'];
  $specific1 = $_POST['specific-text-1']; // THIS IS TEXT I WANT TO USE IF 
  $specific2 = $_POST['specific-text-2']; // THIS IS TEXT I WANT TO USE IF 
  $mailTo = "mail@mymail.com";

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

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

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

My question is, 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?

Sorry if I couldn't find answer but there is such, I'm really new to PHP and Javascript. Thank you all very much!

Aucun commentaire:

Enregistrer un commentaire