我在Mozilla Firefox中遇到问题,当我批准任命讲师角色时,AJAX部分会警告错误消息而不是成功消息。 Chrome和Edge都不会发生这种情况。当我尝试检查网络的响应时,Mozilla Firefox没有任何反应,并且警报自动关闭。但是,无论Web浏览器如何,都可以将电子邮件发送给收件人,并更新数据库。我已经尝试通过PHP文件中的var_dump以及AJAX部分中的JSON.stringify()检查响应,以检查传回但失败的数据。我有三个彼此相关的文件来批准约会。
i)ajax-approve-detail-appointment.php(此文件用于调用模式框,并将所有值放在其中,然后传递给ajax-approve-appointment.php) < / p>
<?php
require ("global-include.php");
if (isset($_POST['appointment_id']))
{
try
{
$fetch_query = "SELECT * FROM appointment
LEFT JOIN users AS student ON appointment.student_id = student.username
LEFT JOIN course_field AS field ON appointment.field = field.field_id
WHERE appointment.lecturer_id = :lecturer AND appointment_id = :appointment_id";
$stmt = $db->prepare($fetch_query);
$stmt->bindParam(':lecturer', $_SESSION['username']);
$stmt->bindParam(':appointment_id', $_POST['appointment_id']);
$stmt->execute();
while($data = $stmt->fetch(PDO::FETCH_ASSOC))
{
$appointment_id = $data['appointment_id'];
$appointment_type = $data ['appointment_type'];
$appointment_date = $data['appointment_date'];
$appointment_time = $data['appointment_time'];
$name = $data['name'];
$email = $data['email'];
$venue = $data['venue'];
$remarks = $data['remarks'];
$field_name = $data['field_name'];
}
}
catch(PDOException $e)
{
echo $e->getMessage();
}
}
?>
<input type='hidden' value="<?php echo $appointment_id; ?>" name='appointment_id' id='appointment_id' />
<input type='hidden' value="<?php echo $appointment_type; ?>" name='appointment_type' id='appointment_type' />
<input type='hidden' value="<?php echo $appointment_date; ?>" name='appointment_date' id='appointment_date' />
<input type='hidden' value="<?php echo $appointment_time; ?>" name='appointment_time' id='appointment_time' />
<input type='hidden' value="<?php echo $name; ?>" name='name' id='name' />
<input type='hidden' value="<?php echo $venue; ?>" name='venue' id='venue' />
<input type='hidden' value="<?php echo $email; ?>" name='student_email' id='student_email' />
<input type='hidden' value="<?php echo $remarks; ?>" name='remarks' id='remarks' />
<input type='hidden' value="<?php echo $field_name; ?>" name='field_name' id='field_name' />
<?php
// Retrieve the columns from the database to get lecturer name
$sql = "SELECT username, name FROM users
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'];
echo "
<input type='hidden' name='lecturer_name' id='lecturer_name' value='". $row['name'] ."' />
<input type='hidden' name='lecturer_username' id='lecturer_username' value=". $row['username'] ." />
";
}
}
?>
<p>Are you sure you want to approve the appointment with this student?</p>
ii)ajax-approve-appointment.php(此文件包含PHPMailer和用于更新数据库的查询)
<?php
require ("global-include.php");
require ('PHPMailer/PHPMailerAutoLoad.php');
require ('PHPMailer/smtp-credentials.php');
if (isset($_POST['appointment_id']))
{
try
{
//fetch data
//vardump($_POST['appointment_id']);
//$appointment_id = $_POST['appointment_id'];
$appointment_type = $_POST ['appointment_type'];
$appointment_date = $_POST['appointment_date'];
$appointment_time = $_POST['appointment_time'];
$name = $_POST['name'];
$venue = $_POST['venue'];
$remarks = $_POST['remarks'];
$field_name = $_POST['field_name'];
$lecturer_name = $_POST['lecturer_name'];
$student_email= $_POST['student_email'];
$approve_appointment_query = "UPDATE appointment SET appointment_status='Approved' WHERE appointment_id= :appointment_id";
$approve_statement= $db->prepare($approve_appointment_query);
$approve_statement->bindParam(':appointment_id', $_POST['appointment_id']);
$approve_statement->execute();
//die($approve_statement);
//PHPMailer set up
$approve_output="<strong>Greetings from STULEC,</strong> <br/>
<p>Dear " . $name . ", your appointment with the lecturer, " . $lecturer_name . ", has been approved. </p>
The appointment details are such as below: <br/>
<ul>
<li>Appointment Type: ". $appointment_type ."</li>
<li>Appoinment Date: ". $appointment_date ."</li>
<li>Appointment Time: ". $appointment_time ."</li>
<li>Venue: ". $venue ." </li>
<li>Course Field: ". $field_name ." </li>
<li>Remarks: ". $remarks . "</li>
</ul>
</p>
<p>Please attend the appointment on time. If you wish to change the time of the the appointment, please login into STULEC and reschedule as fast as possible. Thank you.</p>";
$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('stulecappointmentsystem@gmail.com', 'STULEC'); //From sender
$mail->addAddress( $student_email , $name); // Add a recipient
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'STULEC | Approval of Appointment from Lecturer';
$mail->Body = $approve_output;
$mail->CharSet = 'utf-8';
$mail->ContentType = 'text/html';
if (($approve_statement->execute()) && $mail->send())
{
echo 'The appointment is approved and an email has been sent to the student.';
}
else
{
echo 'Error: The appointment cannot be approved and an email has been sent to the student.';
}
$data = $approve_statement->fetchAll();
echo json_encode($data);
}
catch(PDOException $e)
{
echo $e->getMessage();
}
}
else
{
echo 'Nothing is approved';
}
?>
iii)讲者视图-pending-appointment.php(此文件包含AJAX和JQuery部分)
<script>
$(document).ready(function()
{
//fetch details that want to be approved
$(document).on('click','.approve_data', function()
{
var appointment_id = $(this).attr('id');
//var appointment_id = $("#appointment_id").val();
$.ajax({
async: true,
url: "ajax-approve-detail-appointment.php",
type:"post",
data:{appointment_id:appointment_id},
success:function(data)
{
$("#approve_details").html(data);
$("#approveModal").modal('show');
}
});
})
//approve appointment
$(document).on('click','#btnApprove', function()
{
var appointment_id = $("#appointment_id").val();
var appointment_type = $("#appointment_type").val();
var appointment_date = $("#appointment_date").val();
var appointment_time = $("#appointment_time").val();
var name = $("#name").val();
var venue = $("#venue").val();
var remarks = $("#remarks").val();
var field_name = $("#field_name").val();
var lecturer_name = $("#lecturer_name").val();
var student_email = $("#student_email").val();
$.ajax({
url: "ajax-approve-appointment.php",
type:"post",
data:{appointment_id:appointment_id, appointment_type:appointment_type, appointment_date:appointment_date, appointment_time:appointment_time, name:name, venue:venue, remarks:remarks, field_name:field_name, student_email:student_email, lecturer_name: lecturer_name},
success:function(data)
{
alert('The appointment with this student is successfully approved.');
$("#approveModal").modal('show');
location.reload();
},
error: function(data)
{
//JSON.stringify(data);
alert('Error: The appointment with this student cannot be approved!');
$("#approveModal").modal('show');
location.reload();
}
});
})
});
</script>
从ajax-appprove-detail-appointment.php中,网络响应如下所示:
从ajax-approve-appointment.php中,网络响应都不存在:
这是ajax-approve-appointment.php的参数网络:
希望您能向我指出问题所在,因为我并不是AJAX和JQuery的真正专家。谢谢。