Отправить форму на электронную почту - PullRequest
0 голосов
/ 27 апреля 2020

Привет, ребята. Я изо всех сил пытаюсь выбрать значение datepicker в PHP, оно всегда говорит, что сообщение не может быть отправлено Я не могу найти проблему. Ребята, вы знаете, стоит ли мне обязательно выбирать jquery функцию в PHP.

Я хочу, чтобы после заполнения формы она отправляла все значения, которые были заполнены и выбраны, на мой электронный адрес.

<?php
$error = ""; $successMessage = "";   

if ($_SERVER['REQUEST_METHOD'] === "POST") {      
 if(!$_POST['name']) {

   $error .= "<li>Please introduce a name.</li>";
 }
 if (!$_POST['date']) {
   $error .= "<li>Please pick a date.</li>";
}
if (!$_POST['email'] && filter_var($_POST["email"], FILTER_VALIDATE_EMAIL) === false) {
   $error .= "<li>Invalid Email.</li>";  
}
if ($error != "") {
   $error = '<div class="alert alert-danger d-inline-block mt-0" role="alert">
<p>There were error(s) in your form:</p>' . $error . '</div>';
} else { 

$mailTo = "me@domain.com"; 
$name = $_POST['name'];
$date = $_POST['date'];
$member = $_POST['member'];
$headers = "From: ".$_POST['email'];

 if (mail($mailTo, $name, $date, $member, $headers)) {
   $successMessage = '<div class="alert alert-success d-inline-block" role="alert">Your message was sent, we\'ll get back to you ASAP!</div>';
 } else {
$error = '<div class="alert alert-danger pt-4 d-inline-block" role="alert"><p><strong>Your message couldn\'t be sent - please try again later</div>';
    }
  }    
}
?>

HTML код

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

    <!--Jquery-->
    <link rel="stylesheet" href="js/jquery-ui-1.12.1/jquery-ui.css">
    <link rel="stylesheet" href="js/jquery-ui-1.12.1/jquery-ui.structure.css">
    <link rel="stylesheet" href="js/jquery-ui-1.12.1/jquery-ui.theme.css">
    <!-- CSS -->
    <link rel="stylesheet" href="css/nav.css">
    <link rel="stylesheet" href="css/attendance.css">

    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">

    <!-- Font Awesome -->
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">

    <!-- Fonts -->
    <link href="https://fonts.googleapis.com/css2?family=Poppins:wght@200;300;500&display=swap" rel="stylesheet">

    <title>YesCom - Home</title>
</head>
<body>
<div class="navigation fixed-top">
    <nav class="navbar navbar-light bg-light text-center px-3" id="header">
        <a class="navbar-brand text-center p-3 pl-4 lead" href="#"><span class="yes text-danger border-danger h3" id="nav-logo-yes">Yes</span><span id="nav-logo-com" class="com text-warning border-warning h3">Com</span></a>
      </nav>

      <div class="bor text-center"></div>

      <!-- Menu -->
      <nav class="navbar navbar-expand-lg navbar-light bg-light">
        <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
          <span class="navbar-toggler-icon text"></span>
        </button>
        <div class="collapse navbar-collapse" id="navbarNav">
          <ul class="navbar-nav">
            <li class="nav-item active">
              <a class="nav-link" href="home.html">Home</a>
            </li>
            <li class="nav-item">
              <a class="nav-link" href="#">About</a>
            </li>
            <li class="nav-item">
              <a class="nav-link" href="#">Events</a>
            </li>
              <li class="nav-item">
                <a class="nav-link" href="#">Blog</a>
              </li>
              <li class="nav-item">
                <a class="nav-link" href="#">Forum</a>
              </li>
              <li class="nav-item">
                <a class="nav-link" href="attendance.php">Attendance</a>
              </li>
              <li class="nav-item">
                <a class="nav-link" id="contact-m" href="php/index.php">Contact</a>
              </li>
          </ul>
        </div>
        <hr class="d-xl-none d-md-block d-sm-block text-center">
        <form class="form-inline pull-sm-right d-md-block text-center">
          <input class="form-control" type="email" placeholder="Email">
          <input class="form-control" type="password" placeholder="Password">
          <button class="btn btn-success my-2" id="login-btn" type="submit">Log in</button>
        </form>
      </nav>
    </div>

    <!-- Video header -->

        <div class="container-fluid text-center" id="attendform" style="top: 50%;transform: translateY(25%) !important;position: relative;">    
            <!--Form for join us-->
            <div class="container bg-light p-5 mt-5 d-inline-block text-center">
            <h2 class="join-title text-center display-4 mb-0">Join us!</h2><br><br>

              <div class="mt-0 mb-3" id="error"><? echo $error.$successMessage; ?></div>

                  <div class="container text-center">
                    <form class="" method="post">
                      <label for="name" class="text-dark border-top-0 border-right-0 border-left-0" >Name: </label> <br>
                      <input class="form-control" type="text" name="name" id="name" placeholder="Bro./Sis."><br>

                      <label for="email">Email</label>
                      <input type="email" name="email" class="form-control" id="email" placeholder="user@domain.com" required> <br>
                  <!---->
                        <label for="date" class="d-inline-block">Date: </label> 
                        <input type="text" id="datepicker" name="date" class="form-control"></input></p><br>

                      <label for="member">Are you already a member of the youth?</label><br>
                      <input type="radio" name="member" value="Yes">Yes
                      <input type="radio" name="member" value="No">No <br><br>

                      <input class="btn btn-dark" id="sendbtn" type="submit" value="Send">
                      </form>
                    </div>
                </div>
            </div>


                </form>
            </div>
        </div>
<div id="myDiv"></div>
    <!-- jQuery first, then Popper.js, then Bootstrap JS -->
    <script src="https://code.jquery.com/jquery-3.4.1.slim.min.js" integrity="sha384-J6qa4849blE2+poT4WnyKhv5vZF5SrPo0iEjwBvKU7imGFAV0wwj1yYfoRSJoZ+n" crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js" integrity="sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6" crossorigin="anonymous"></script>
    <!--script-->
    <script src="js/jquery-3.4.1.min.js"></script>
    <script src="https://code.jquery.com/ui/1.12.0/jquery-ui.js" integrity="sha256-0YPKAwZP7Mp3ALMRVB2i8GXeEndvCq3eSl/WsAl1Ryk=" crossorigin="anonymous"></script>
    <script type="text/javascript">
          $(function() {
               $("#datepicker").datepicker();
        });
      </script>
  </body>
</html>

1 Ответ

1 голос
/ 28 апреля 2020

Привет. Спасибо за вашу поддержку. Мне удалось отладить, и вот мой ответ:

HTML ФОРМА:

<div class="container-fluid text-center" id="attendform" style="top: 50%;transform: translateY(30%) !important;position: relative;">    
            <!--Form for join us-->
            <div class="container-fluid bg-light p-5 mt-5 d-inline-block text-center">
            <h2 class="join-title text-center display-4 mb-0">Join us!</h2><br><br>

              <div class="mt-0 mb-3" id="error"><? echo $error.$success; ?></div>

                  <div class="container text-center">
                    <form class="" method="post">
                      <label for="name" class="text-dark border-top-0 border-right-0 border-left-0" >Name: </label> <br>
                      <input class="form-control" type="text" name="name" id="name" placeholder="Bro./Sis."><br>

                      <label for="email">Email</label>
                      <input type="email" name="email" class="form-control" id="email" placeholder="user@domain.com" required> <br>
                  <!---->
                      <p class="font-weight-bold">Week: </p>
                      <select name="week">
                        <option  id="week" value="1st week">1st week</option>
                        <option  id="week" value="2nd week">2nd week</option>
                        <option  id="week" value="3rd week">3rd week</option>
                        <option  id="week" value="4th week">4th week</option>
                        <option  id="week" value="5th week">5th week</option> 
                      </select><br> <br>

                      <label for="days" class="font-weight-bold">Day: </label> <br>
                        <input type="checkbox" name="day" id="day" value="Saturday">Saturday
                        <input type="checkbox" name="day" id="day" value="Sunday">Sunday
                        <input type="checkbox" name="day" id="day" value="Both">Both
                        <input type="checkbox" name="day" id="day" value="None">None <br><br>


                      <p>Are you already a member of the youth?</p>
                      <label for=member>
                      <input type="radio" name="member" value="Yes member">Yes
                      <input type="radio" name="member" value="Non member">No 
                      </select><br><br>
                      <input class="btn btn-dark" id="sendbtn" type="submit" name="send" value="Send">
                      </form>
                    </div>
                </div>
            </div>

PHP код:

<?php


if (isset($_POST['send'])) {      
  $name = $_POST['name'];
  $email = $_POST['email'];
  $subject = "Attendance Form";
  $date = $_POST['date'];
  $week = $_POST['week'];
  $day = $_POST['day'];
  $member = $_POST['member'];
  $message = "Bro./Sis. ".$name." "."($member)"."\n\n"."Schedule:"."\n\n"."Time of the week: ".$week."\n"."Day: ".$day;


  $emailTo = "me@domain.com";
  $mailHeaders = "From: " . $name . "<". $email .">\r\n";
  if (mail($emailTo, $subject, $message, $mailHeaders)) {
    $success ='<div class="alert alert-success d-inline-block" role="alert">Your message has been sent.</div>';
    $type = "success";
  } else {
    $error = '<div class="alert alert-danger d-inline-block" role="alert">Something went wrong, please try again.</div>';
  }
}

?>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...