PHP $ _POST值未出现在PHPMailer的电子邮件中

时间:2019-03-22 15:40:11

标签: php pdo phpmailer

问候大家

我有一个问题,在我通过PHPMailer发送电子邮件后,学生姓名和程序的值没有出现在电子邮件中。我正在使用PHP PDO,并且已经检查了学生姓名和程序是否在表单下。其他值在电子邮件中显示的很好,例如下面的图片: image

我不确定是引号引起问题还是其他原因。 下面是我的student-appointment-form.php文件的代码:

<?php
  require ("global-include-student.php");
  require ('PHPMailer/PHPMailerAutoLoad.php');
  require ('PHPMailer/smtp-credentials.php');

  //timezone for Malaysia
  date_default_timezone_set("Asia/Kuala_Lumpur");

  //Condition when the submit button is clicked
  if(isset($_POST["btnSubmit"])){
    try 
    {
      $appointment_date = date('Y-m-d',strtotime($_POST['appointment_date']));
      //$appointment_date = date("Y-m-d H:i:s");

      // Inserting the data into database
      $insert_query = "INSERT INTO appointment (appointment_type, appointment_date, appointment_time, student_id, lecturer_id, field, venue, remarks, appointment_status)
                        VALUES ('".$_POST["appointment_type"]."',
                                '".$appointment_date."',
                                '".$_POST["appointment_time"]."',
                                '".$_POST["student_id"]."',
                                '".$_POST["lecturer_id"]."',
                                '".$_POST["field_id"]."',
                                '".$_POST["venue"]."',                 
                                '".$_POST["remarks"]."',
                                '".$_POST["appointment_status"]."'                                       
                                )";

      //PHPMailer set up
      $field_name = $_POST['field_name'];
      $lect_name = $_POST['lect_name'];
      $lect_email = $_POST['lect_email'];
      $student_name = $_POST['student_name'];
      $programme = $_POST['programme'];

      $output='<strong>Greetings from STULEC,</strong> <br/>
              <p>There is an appointment request from ' . $student_name . ' who is in the ' . $programme . ' programme.</p> 
              The appointment details are such as below: <br/>
                <ul>
                  <li>Appointment Type: '. $_POST['appointment_type'] .'</li>
                  <li>Appoinment Date: '. $_POST['appointment_date'] .'</li>
                  <li>Appointment Time: '. $_POST['appointment_time'] . '</li>
                  <li>Venue: '. $_POST['venue'] .' </li>
                  <li>Course Field: '. $_POST['field_name'] .' </li>
                  <li>Remarks: '. $_POST['remarks'] . '</li>
                </ul>
              </p>
              <p>Please approve the appointment request by logging into STULEC. Thank you.';

      $mail = new PHPMailer;
      $mail->isSMTP();                                // Set mailer to use SMTP
      $mail->Host = 'smtp.gmail.com';             // Specify main and backup SMTP servers
      $mail->SMTPAuth = true;                   // Enable SMTP authentication
      $mail->Username = EMAIL;              // SMTP username
      $mail->Password = PASS;               // SMTP password
      $mail->SMTPSecure = 'tls';                    // Enable TLS encryption, `ssl` also accepted
      $mail->Port = 587;                        // TCP port to connect to

      $mail->setFrom('stulec@edu.my', 'STULEC');  //From sender
      $mail->addAddress($lect_email, $lect_name);  // Add a recipient
      $mail->isHTML(true);                    // Set email format to HTML

      $mail->Subject = 'STULEC | Appointment Request from Student';
      $mail->Body    =  $output;
      $mail->CharSet = 'utf-8';
      $mail->ContentType = 'text/html';                          

      if (($db->query($insert_query)) && $mail->send())
      {
        echo "<script type= 'text/javascript'>alert('An appointment has been made with the lecturer. Please wait until the lecturer responds to your request.');";
        echo 'window.location= "student-view-pending-appointment.php"';
        echo "</script>";
      }                                          
      else
      {
        echo "<script type= 'text/javascript'>alert('Error: The appoinment cannot be made!');";
        echo 'window.location= "student-appointment-form.php"';
        echo "</script>";
      }
    }
    catch(PDOException $e)
    {
      echo $e->getMessage();
    }
  }

?>   

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <title>STULEC | Appointment Form</title>
</head>
<body class="hold-transition skin-blue sidebar-mini">
<div class="wrapper">

 <!--Include header-->
 <?php include 'include/header.php' ?>

  <!--Include sidebar-->
  <?php include 'include/sidebar.php' ?>

  <!-- Content Wrapper. Contains page content -->
  <div class="content-wrapper">
    <!-- Content Header (Page header) -->
    <section class="content-header">
      <h1 align='center'>
        <u>Appointment Form</u>
      </h1>
    </section>

    <!-- Main content -->
    <section class="content">
      <div class="box box-default">
        <div class="box-body">
          <div class="row">
            <div class="col-md-12">
                  <!-- form start -->
                  <form class="form-horizontal" action="" method="POST">
                  <?php
                     //select student details from the database
                    $sql = "SELECT username, name, role, department, programme, profile_pic, programme_name FROM users 
                    LEFT JOIN programme AS programme ON programme.programme_id = users.programme
                    WHERE user_id = :user_id";        

                    $stmt = $db->prepare($sql);
                    if ($stmt->execute(array('user_id' => $_SESSION['user_id'])))
                    {
                      while($row = $stmt->fetch()) 
                      {
                      //fetch the rows
                        $username = $row['username'];
                        $name = $row['name'];
                        $programme = $row['programme_name'];

                        echo " 
                        <!--Name-->
                        <div class='form-group'>
                          <label for='Name' class='col-sm-2 control-label'>Name</label>
                          <div class='col-sm-10'>
                            <input type='text' class='form-control' name='student_name' id='student_name' value='". $row['name']."' disabled>
                            <input type='hidden' class='form-control' name='student_id' id='student_id' value=" . $row['username'] ." />
                          </div>
                        </div>

                        <!--Programme-->
                        <div class='form-group'>
                          <label for='Programme' class='col-sm-2 control-label'>Programme</label>
                          <div class='col-sm-10'>
                            <input type='text' class='form-control' name='programme' id='programme' value=" . $row['programme_name'] ." disabled>
                          </div>
                        </div>
                        ";
                      }
                    }
                  ?>

                    <!--Consultation Type-->
                    <div class='form-group'>
                      <label for='ConsultationType' class='col-sm-2 control-label'>Consultation Type</label>
                      <div class='col-sm-10'>
                        <select class='form-control' name='appointment_type'>
                          <option selected='selected' name='assignment_discussion' value='Assignment Discussion'>Assignment Discussion</option>
                          <option name='advisory_session' value='Advisory Session'>Advisory Session</option>
                          <option name='fyp_discuss' value='Final Year Project Discussion'>Final Year Project Discussion</option>
                        </select>
                      </div>
                    </div>

                    <!--Field name-->
                    <div class='form-group'>
                      <label for='Course' class='col-sm-2 control-label'>Course Field</label>
                      <div class='col-sm-10'>
                        <select class='form-control' name='field_id' id='field_id' required>  
                          <option value=''>Please select a course field</option>         
                          <?php
                            //Display field name
                            $select_field_query = "SELECT field_id, field_name FROM course_field 
                                                    LEFT JOIN field_programme ON field_programme.field = course_field.field_id
                                                    LEFT JOIN users ON users.programme = field_programme.programme
                                                    LEFT JOIN programme ON programme.programme_id = field_programme.programme
                                                    WHERE field_programme.programme = :programme AND users.username = :student";
                            $field_statement = $db->prepare($select_field_query);
                            $field_statement->bindParam(':programme', $_SESSION['programme']);
                            $field_statement->bindParam(':student', $_SESSION['username']);
                            $field_statement->execute();

                            while ($row = $field_statement->fetch(PDO::FETCH_ASSOC)) 
                            {
                              echo 
                              "<option value='" . $row['field_id'] . "'>" . $row['field_name'] . "</option>";
                            }
                          ?>   
                        </select>
                      </div>
                    </div>

                    <!--Hidden course field_name-->
                    <input type='hidden' id='field_name' name='field_name'>

                    <!--Lecturer name selection-->
                    <div class='form-group'>
                      <label for='Lecturer' class='col-sm-2 control-label'>Lecturer</label>
                      <div class='col-sm-10'>
                        <select class='form-control' name='lecturer_id' id='lecturer_id'>
                            <option>-</option>
                        </select>
                      </div>  
                    </div>

                    <!--Hidden lecturer email-->
                    <input type='hidden' id='lect_email' name='lect_email'>

                    <!--Hidden lecturer name-->
                    <input type='hidden' id='lect_name' name='lect_name'>        

                    <!-- Date -->
                    <div class='form-group'>
                      <label for='Date' class='col-sm-2 control-label'>Date</label>
                      <div class='col-sm-4'>
                        <div class='input-group date'>
                          <input type='text' class='form-control pull-right' name='appointment_date' id='datepicker' required>
                          <div class='input-group-addon'>
                            <i class='fa fa-calendar'></i>
                          </div>
                        </div><!-- /.input group -->
                      </div><!-- /.form group -->    
                    </div>

                    <!-- Time -->
                    <div class='form-group'>
                      <label for='Time' class='col-sm-2 control-label'>Time</label>
                      <div class='col-sm-4'>
                        <div class='input-group time'>
                          <input type='text' class='form-control timepicker' name='appointment_time' id='timepicker' required>
                          <div class='input-group-addon'>
                            <i class='fa fa-clock-o'></i>
                          </div>
                        </div><!-- /.input group -->
                      </div>
                    </div><!-- /.form group -->

                    <!--Venue-->
                    <div class='form-group'>
                      <label for='Venue' class='col-sm-2 control-label'>Venue</label>
                      <div class='col-sm-10'>
                        <select class='form-control' name='venue'>
                          <option selected='selected' name='in_front_dpmt' value='In front of department'>In front of department</option>
                          <option name='library' value='Library'>Library</option>
                          <option name='consultation_room' value='Consultation Room'>Consultation Room</option>
                        </select>
                      </div>
                    </div>

                    <!--Remarks-->
                    <div class='form-group'>
                      <label for='Remarks' class='col-sm-2 control-label'>Remarks</label>
                      <div class='col-sm-10'>
                        <input type='text' class='form-control' name='remarks' id='remarks' placeholder='Eg: Related to the Local Area Network question' required>
                      </div>
                    </div> 

                    <!--Hidden Appointment Status which is set as Pending when form is submitted-->
                    <input type="hidden" value="Pending" name="appointment_status">

                    <!--Submit button-->
                    <div class="box-footer">
                      <button type="submit" class="btn btn-success pull-right" name="btnSubmit" id="btnSubmit">Submit</button>
                    </div> 
                  </form>
                </div><!-- /.box -->
            </div><!-- /.col -->
            </div><!-- /.col -->
          </div><!-- /.row -->
        </div><!-- /.box-body -->
      </div><!-- /.box -->

</div><!-- ./wrapper -->


<!-- Page script -->
<script>
  $(function () 
  {
    //Change lecturer when different course field is selected
    $("#field_id").change(function () {
      if ($('#field_id option:selected').val() != "")
      {
        $.ajax({
            type: 'POST',
            url: 'ajax-field-lecturer.php',
            data: { 'field_id' : $('#field_id option:selected').val() },
            success: function(data) {
                $('#lecturer_id').empty();
                var lect = JSON.parse(data);
                $.each(lect, function( index, lectObj ) 
                {
                  if (index == 0)
                  {
                    $('#lect_email').val(lectObj.email);
                    $('#lect_name').val(lectObj.name);
                    $('#field_name').val(lectObj.field_name);
                  }
                    $('#lecturer_id').append("<option value='"+lectObj.username+"'>"+lectObj.name+"</option>");
                });
            },
            error: function(data) {
                alert(data);
            }
        });
      }
      else
      {
        $('#lecturer_id').empty();
      }  
    });

    //Change lecturer email when different the lecturer is selected
    $("#lecturer_id").change(function () {
      if ($('#lecturer_id option:selected').val() != "")
      {
        $.ajax({
            type: 'POST',
            url: 'ajax-email-lecturer.php',
            data: { 'lecturer_id' : $('#lecturer_id option:selected').val() },
            success: function(data) {
                //alert(data);
                $('#lect_email').empty();

                var lect = JSON.parse(data);
                $('#lect_email').val(lect[0].email);
            },
            error: function(data) {
                alert(data);
            }
        });
      }
      else
      {
        $('#lect_email').empty();
      }  
    });

    //Show lecturer name
    $("#lecturer_id").change(function () {
      if ($('#lecturer_id option:selected').val() != "")
      {
        $.ajax({
            type: 'POST',
            url: 'ajax-name-lecturer.php',
            data: { 'lecturer_id' : $('#lecturer_id option:selected').val() },
            success: function(data) {
                //alert(data);
                $('#lect_name').empty();

                var lect = JSON.parse(data);
                $('#lect_name').val(lect[0].name);
            },
            error: function(data) {
                alert(data);
            }
        });
      }
      else
      {
        $('#lect_name').empty();
      }  
    });

    //Show course field name
    $("#field_id").change(function () {
      if ($('#field_id option:selected').val() != "")
      {
        $.ajax({
            type: 'POST',
            url: 'ajax-field-name.php',
            data: { 'field_id' : $('#field_id option:selected').val() },
            success: function(data) {
                //alert(data);
                $('#field_name').empty();

                var lect = JSON.parse(data);
                $('#field_name').val(lect[0].field_name);
            },
            error: function(data) {
                alert(data);
            }
        });
      }
      else
      {
        $('#field_name').empty();
      }  
    });

    //Date picker
    $( "#datepicker" ).datepicker({
    dateFormat: "yyyy-MM-dd"
    });

    //Timepicker
    $('.timepicker').timepicker({
      showInputs: false
    })
  })
</script>
</body>
</html>

请指导我如何解决此问题。谢谢。

1 个答案:

答案 0 :(得分:3)

在您的表单中,您的var arr1=[3, 'a', 'a', 'a', 2, 3, 'a', 3, 'a', 2, 4, 9, 3, 'A']; var uniqueArray = arr1.filter(function(elem,i,rep){ console.log("index :"+i+" first indext of this value: "+rep.indexOf(elem)+" element :"+elem+ " is filterable :"+(i == rep.indexOf(elem)) ); return i == rep.indexOf(elem); }) console.log(uniqueArray);student_name文件是programme,没有发布。 考虑使用disabled输入值或将hidden更改为disable(不确定后者是否可以正常工作)。