[英]Adding in a PHP email to a file that has info sent with an AJAX call killing code
我創建了更新用戶組級別(權限級別)的代碼。 我通過AJAX將選定的ID和組級別發送到名為user_update_group的php文件。 在嘗試將PHP電子郵件添加到同一文件之前,更新用戶的組#效果很好。 我這樣做是因為我已經從數據庫中獲取了該用戶的信息,所以我認為這是最好的方法。
但是,在添加電子郵件時會破壞代碼。 我的php文件中是否有某些東西顯然可以殺死它,或者我不能這樣做嗎? 我試圖從帶來的ID中SELECT
用戶表中的所有數據,然后使用與其他查詢相同的按鈕向他們發送電子郵件。 我的php電子郵件是我在此處添加的php文件的第二部分。 我添加了所有這些代碼以顯示我要執行的操作。
<?php
$con2 = mysqli_connect("localhost", "root", "", "db");
$run2 = mysqli_query($con2,"SELECT * FROM user_requests ORDER BY id DESC");
$runUsers2 = mysqli_query($con2,"SELECT * FROM users ORDER BY id DESC");
$numrows2 = mysqli_num_rows($run2);
if( $numrows2 ) {
while($row2 = mysqli_fetch_assoc($run2)){
if($row2['status'] == "Approved"){
//var_dump ($row2);
$approved_id = $row2['user_id'];
$approved_firstname = $row2['firstname'];
$approved_lastname = $row2['lastname'];
$approved_username = $row2['username'];
if ($approved_firstname == true) {
echo "Name - ". $approved_firstname . " " . $approved_lastname . "
</br>" .
"Username - ". $approved_username . "</br></br>"
?>
<div class="change_group_button">
<a class="change_group" href="javascript:void(0)">Change User Permission</a>
</div><br>
<div id="light" class="change_group_popup">
<a class="close" href="javascript:void(0)">Close</a>
<div class="group_success" style="color: red;"></div><br>
<form id="update_group" action="" method="POST" accept-charset="utf-8">
<div class="field">
<label for="group">Group</label>
<input type="hidden" value="<?php echo $approved_id; ?>" id="approved_id" name="id" />
<select id='group_id' name='group' required>
<option value=''><?php echo htmlentities($group); ?></option>
<option value="1">Bench</option>
<option value="2">Spectator</option>
<option value="3">Team Member</option>
<option value="4">Commissioner</option>
</select>
</div>
<input type="submit" value="submit" name="group">
</form>
AJAX電話
$(document).ready(function () {
$('#update_group').on('submit', function (event) {
event.preventDefault();
$.ajax({
url: 'user_group_update.php',
type: 'POST',
data: {
id: $("#approved_id").val(), //id
// update_group: $("#group_id").val() //group level
update_group: $(this).find( "#group_id option:selected" ).val()
},
success: function (data) {
//do something with the data that got returned
$(".group_success").fadeIn();
$(".group_success").show();
$('.group_success').html('User Permission Level Changed!');
$('.group_success').delay(5000).fadeOut(400);
// alert(data);
},
error: function(jqXHR, textStatus,errorThrown )
{
// alert on an http error
alert( textStatus + errorThrown );
}
});
return false;
});
});
user_group_update php文件。
$approved_id = $_POST['id'];
$change_group = $_POST['update_group'];
$con = mysqli_connect("localhost","root","","db");
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$stmt = $con->prepare("UPDATE users SET `group`=? WHERE id=?");
if ( !$stmt || $con->error ) {
// Check Errors for prepare
die('User Group update prepare() failed: ' . htmlspecialchars($con->error));
}
if(!$stmt->bind_param('ii', $change_group, $approved_id)) {
// Check errors for binding parameters
die('User Group update bind_param() failed: ' . htmlspecialchars($stmt->error));
}
if(!$stmt->execute()) {
die('User Group update execute() failed: ' . htmlspecialchars($stmt->error));
}
//-------Email test--------------
$email_stmt = $con->prepare("SELECT * FROM users WHERE id=?");
if ( !$email_stmt || $con->error ) {
// Check Errors for prepare
die('User email prepare() failed: ' . htmlspecialchars($con->error));
}/*
if(!$email_stmt->bind_param('ii', $change_group, $approved_id)) {
// Check errors for binding parameters
die('User email bind_param() failed: ' . htmlspecialchars($stmt->error));
}
if(!$email_stmt->execute()) {
die('User email execute() failed: ' . htmlspecialchars($stmt->error));*/
/*$pending_id = $_POST['id'];
$group_firstname = $_POST['firstname'];
$group_lastname = $_POST['lastname'];
$group_username = $_POST['username'];
$group_email = $_POST['email'];
$group_email = $_POST['group'];
$to = $group_email;
$subject = 'There is a new user request to join the Sunday Funday League';
$message = '
<html>
<head>
<title>New SFL User Request</title>
</head>
<body>
<p>Hi '.$group_firstname.',</p><br>
<p>Your Sunday Funday League Account has been accepted. You have been added to the group. To sign in, click this link
http://sundayfundayleague.com . </p><br>
<p>Thank you,</p>
<p>Administration</p>
</body>
</html>
';
$from = "user-requests@sundayfundayleague.com";
$Bcc = "user-requests-confirm@sundayfundayleague.com";
// To send HTML mail, the Content-type header must be set
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// Additional headers
$headers .= 'To: ' .$to. "\r\n";
$headers .= 'From: ' .$from. "\r\n";
$headers .= 'Bcc: '.$Bcc. "\r\n";
// Send the email
mail($to,$subject,$message,$headers);
}*/
user_update_group文件的更新代碼
$approved_id = $_POST['id'];
//test - delete if it doesn't work
$approved_firstname = $_POST['firstname'];
$approved_lastname = $_POST['lastname'];
$approved_username = $_POST['username'];
$approved_email = $_POST['email'];
$change_group = $_POST['update_group'];
$con = mysqli_connect("localhost","root","","db");
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$stmt = $con->prepare("UPDATE users,user_requests SET users.group=?, user_requests.group=? WHERE users.id=? AND user_requests.user_id=?");
if ( !$stmt || $con->error ) {
// Check Errors for prepare
die('User Group update prepare() failed: ' . htmlspecialchars($con->error));
}
if(!$stmt->bind_param('iiii', $change_group, $change_group, $approved_id, $approved_id)) {
// Check errors for binding parameters
die('User Group update bind_param() failed: ' . htmlspecialchars($stmt->error));
}
if(!$stmt->execute()) {
die('User Group update execute() failed: ' . htmlspecialchars($stmt->error));
}
//test
$email_stmt = $con->prepare("SELECT * FROM users WHERE id=?");
if ( !$email_stmt || $con->error ) {
// Check Errors for prepare
die('User email prepare() failed: ' . htmlspecialchars($con->error));
}
/*if(!$email_stmt->bind_param('ii', $change_group, $approved_id)) {
// Check errors for binding parameters
die('User email bind_param() failed: ' . htmlspecialchars($stmt->error));
}
if(!$email_stmt->execute()) {
die('User email execute() failed: ' . htmlspecialchars($stmt->error));*/
$to = $approved_email;
$subject = 'There is a new user request to join t';
$message = '
<html>
<head>
<title>New User Request</title>
</head>
<body>
<p>Hi '.$approved_firstname.',</p><br>
<p>Your Account has been accepted. You have been added to the group. To sign in, click this link
http://example.com . </p><br>
<p>Thank you,</p>
<p>Administration</p>
</body>
</html>
';
$from = "user-requests@example.com";
$Bcc = "user-requests-confirm@example.com";
// To send HTML mail, the Content-type header must be set
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// Additional headers
$headers .= 'To: ' .$to;//. "\r\n";
$headers .= 'From: ' .$from;//. "\r\n";
$headers .= 'Bcc: '.$Bcc;//. "\r\n";
// Send the email
mail($to,$subject,$message,$headers);
_POST由瀏覽器在提交表單時自動生成。 如果要通過帖子傳遞數據,則必須使用表單提交字段。 這些字段可以隱藏:
<input type=hidden name="lastname" value="{some_last_name}"/>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.