File: /var/www/test/changeemail.php
<?php
session_start();
if (!isset($_SESSION['user'])) {
header('Location: login.php');
exit();
}
$email = $_SESSION['email'];
$error = '';
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$newEmail = trim($_POST['new-email']);
$confirmEmail = trim($_POST['confirm-email']);
if ($newEmail == $email) {
$error = "新電子信箱不能與目前的電子信箱相同。";
} elseif ($newEmail != $confirmEmail) {
$error = "兩次輸入的電子信箱不一致。";
} else {
require_once 'db.php'; // 資料庫連線
$user = $_SESSION['user'];
$sql = "UPDATE filemaker SET email = ? WHERE user = ?";
$stmt = $conn->prepare($sql);
if ($stmt === false) {
$error = "SQL 預備語法錯誤: " . $conn->error;
} else {
$stmt->bind_param('ss', $newEmail, $user);
if ($stmt->execute()) {
$_SESSION['email'] = $newEmail;
$subject = "亞訊寬頻會員電子信箱修改通知";
$fromEmail = "service@ysnet.com.tw";
$headers = "From: $fromEmail\r\nReply-To: $fromEmail\r\nContent-Type: text/plain; charset=UTF-8\r\n";
$message = "您好,您新修改的電子信箱為:\n $newEmail \n\n亞訊寬頻 客服中心";
if (mail($newEmail, $subject, $message, $headers)) {
header('Location: member.php');
} else {
$error = "電子信箱已變更,但發送通知信失敗,請聯繫客服。";
}
exit();
} else {
$error = "更新失敗: " . $stmt->error;
}
}
}
}
?>
<!DOCTYPE html>
<html lang="zh-TW">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>修改電子信箱</title>
<style>
body {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background-color: #f9f9f9;
font-family: Arial, sans-serif;
}
.container {
background-color: #fff;
padding: 20px 30px;
border-radius: 10px;
box-shadow: 0 4px 10px rgba(0,0,0,0.1);
width: 400px;
}
h2 {
text-align: center;
margin-bottom: 20px;
}
label {
display: block;
margin-bottom: 8px;
font-weight: bold;
}
input[type="email"] {
width: 100%;
padding: 8px;
margin-bottom: 15px;
border: 1px solid #ddd;
border-radius: 5px;
}
.buttons {
display: flex;
justify-content: space-between;
margin-top: 20px;
}
button {
padding: 10px 20px;
border: none;
border-radius: 5px;
font-size: 16px;
cursor: pointer;
transition: background-color 0.3s;
}
.submit-btn {
background-color: #007bff;
color: #fff;
}
.submit-btn:disabled {
background-color: #ccc;
cursor: not-allowed;
}
.submit-btn:hover:enabled {
background-color: #0056b3;
}
.cancel-btn {
background-color: #d9534f;
color: #fff;
}
.cancel-btn:hover {
background-color: #c9302c;
}
</style>
<script src="/js/iframe-resizer-helper.js"></script>
</head>
<body>
<div class="container">
<h2>修改電子信箱</h2>
<form action="" method="POST">
<label for="current-email">目前的電子信箱</label>
<span><?php echo htmlspecialchars($email); ?></span>
<label for="new-email">新電子信箱</label>
<input type="email" id="new-email" name="new-email" placeholder="請輸入新電子信箱" required>
<label for="confirm-email">確認新電子信箱</label>
<input type="email" id="confirm-email" name="confirm-email" placeholder="請再次輸入新電子信箱" required>
<div class="buttons">
<button type="submit" id="submit-btn" class="submit-btn" disabled>修改送出</button>
<button type="button" class="cancel-btn" onclick="window.location.href='member.php'">取消返回</button>
</div>
</form>
</div>
<script>
// 等待頁面渲染完,再主動請父頁更新 iframe 高度
setTimeout(() => {
if (typeof notifyParentOfHeightChange === "function") {
notifyParentOfHeightChange();
}
}, 300); // 延遲 300ms 較穩定,也可視情況改 500
const newEmail = document.getElementById('new-email');
const confirmEmail = document.getElementById('confirm-email');
const submitBtn = document.getElementById('submit-btn');
function validateEmails() {
if (newEmail.value.trim() !== '' && confirmEmail.value.trim() !== '' && newEmail.value === confirmEmail.value) {
submitBtn.disabled = false;
} else {
submitBtn.disabled = true;
}
}
newEmail.addEventListener('input', validateEmails);
confirmEmail.addEventListener('input', validateEmails);
</script>
</body>
</html>