代码拉取完成,页面将自动刷新
<?php
session_start();
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
function checkUserLoggedIn()
{
if (isset($_SESSION['id'])) {
$user_id = (int)$_SESSION['id'];
echo "<!-- User ID: {$user_id} -->";
return $user_id;
} else {
header('Location: login.html'); // 替换为实际的登录页面URL
exit();
}
}
$user_id = checkUserLoggedIn();
// 连接数据库(请替换为实际的数据库连接参数)
$db_host = 'localhost';
$db_name = 'cyberpunk';
$db_user = 'root';
$db_password = 'root';
$pdo = new PDO("mysql:host=$db_host;dbname=$db_name", $db_user, $db_password);
// 编辑地址逻辑
if (isset($_GET['address_id']) && is_numeric($_GET['address_id'])) {
$address_id = (int)$_GET['address_id'];
// 查询要编辑的地址信息
$stmt = $pdo->prepare("SELECT * FROM user_addresses WHERE id = ? AND user_id = ?");
$stmt->execute([$address_id, $user_id]);
$address = $stmt->fetch(PDO::FETCH_ASSOC);
if (!$address) {
echo "<p class='error'>无法找到要编辑的地址,请检查地址ID或重新尝试。</p>";
echo "<!-- SQL Query: SELECT * FROM user_addresses WHERE id = {$address_id} AND user_id = {$user_id} -->";
exit();
}
// 设置省份和城市初始值
$initialProvince = $address['state_province'];
$initialCity = $address['city'];
} else {
echo "<p class='error'>缺少或无效的地址ID,请检查链接或重新尝试。</p>";
exit();
}
// 关闭数据库连接
$pdo = null;
?>
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<link rel="icon" href="img/favicon.ico" type="image/x-icon">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<title>编辑收获地址 | Cyberpunk2077未来车辆</title>
<link
rel="stylesheet"
href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/4.1.1/animate.min.css"
/>
<link rel="stylesheet" href="css/edit_addreass.css" />
<script>
$(document).ready(function() {
// 确保DOM加载完成后再执行以下代码
$("#addressForm").on("submit", function(e) {
e.preventDefault(); // 阻止表单默认提交行为
// 序列化表单数据
var formData = $(this).serialize();
// 使用Ajax发送表单数据到process_address_edit.php
$.ajax({
type: "POST",
url: "process_address_edit.php", // 确保这是正确的处理地址
data: formData,
dataType: "json", // 期望返回的数据类型为JSON
success: function(response) {
if (response.success) {
alert(response.message); // 显示成功消息
// 可以在这里添加更多逻辑,比如刷新页面或跳转
} else {
alert(response.message); // 显示错误消息
}
},
error: function(xhr, status, error) {
if (xhr.status === 400) {
alert('请求错误,请检查输入数据。');
} else if (xhr.status === 500) {
alert('服务器内部错误,请稍后重试。');
} else {
console.error("Ajax请求错误:", status, ", ", error);
alert("地址更新过程中发生了错误,请重试。");
}
}
});
});
});
// 添加全局调试函数
function logDebug(message) {
console.debug("[DEBUG] " + message);
}
function setSelectedOption(selectElement, value) {
const option = selectElement.querySelector(`option[value="${value}"]`);
if (option) {
option.selected = true;
}
}
function loadLocations(callback) {
var xhr = new XMLHttpRequest();
xhr.open('GET', 'locations.json', true);
xhr.onload = function() {
if (xhr.status === 200) {
callback(JSON.parse(xhr.responseText));
} else {
console.error('Failed to load locations data.');
}
};
xhr.send();
}
function loadProvinces(country, initialProvince, initialCity) {
logDebug('Loading provinces for country:', country);
var provinceSelect = document.getElementById('state_province');
provinceSelect.innerHTML = '<option value="">选择省份</option>';
if (country !== '') {
loadLocations(data => {
var provinces = data.countries.find(c => c.name === country).provinces;
logDebug('Found provinces:', provinces);
provinces.forEach(p => {
var option = document.createElement('option');
option.value = p.name;
option.text = p.name;
provinceSelect.appendChild(option);
});
// 设置省份初始值
if (initialProvince) {
setSelectedOption(provinceSelect, initialProvince);
}
// 加载初始城市
loadCities(provinceSelect.value, initialCity);
});
}
}
function getCitiesForProvince(data, province) {
return data.countries.reduce((cities, country) => {
const provinces = country.provinces;
if (provinces && provinces.some(p => p.name === province)) {
return cities.concat(provinces.find(p => p.name === province).cities.map(c => c.name));
}
return cities;
}, []);
}
function loadCities(province, initialCity) {
logDebug('Loading cities for province:', province);
var citySelect = document.getElementById('city');
citySelect.innerHTML = '<option value="">选择城市</option>';
if (province !== '' && province !== '选择省份') {
loadLocations(data => {
var cities = getCitiesForProvince(data, province);
logDebug('Found cities:', cities);
cities.forEach(c => {
var option = document.createElement('option');
option.value = c;
option.text = c;
citySelect.appendChild(option);
});
// 设置城市初始值
if (initialCity) {
setSelectedOption(citySelect, initialCity);
}
});
}
}
window.onload = function() {
loadLocations(function(data) {
var countrySelect = document.getElementById('country');
data.countries.forEach(c => {
var option = document.createElement('option');
option.value = c.name;
option.text = c.name;
countrySelect.appendChild(option);
});
// 设置初始国家
setSelectedOption(countrySelect, "<?php echo htmlspecialchars($address['country']); ?>");
// 加载初始省份和城市
loadProvinces("<?php echo htmlspecialchars($address['country']); ?>", "<?php echo htmlspecialchars($initialProvince); ?>", "<?php echo htmlspecialchars($initialCity); ?>");
});
};
</script>
<script type="text/javascript">
function redirectToPage() {
window.location.href = 'user_addresses_add.php';
}
</script>
</head>
<body>
<video src="video/Cyberpunk_2077_1080.mp4" class="bjimg" autoplay loop muted=></video>
<form id="addressForm" method="POST" enctype="multipart/form-data" class="animate__animated animate__fadeIn">
<input type="hidden" name="address_id" value="<?php echo $address_id; ?>">
<h2 class="neon-effect">编辑收货地址</h2>
<label for="address_line_1">地址1:</label>
<input type="text" id="address_line_1" name="address_line_1" value="<?php echo htmlspecialchars($address['address_line_1']); ?>" required>
<label for="address_line_2">地址2(可选):</label>
<input type="text" id="address_line_2" name="address_line_2" value="<?php echo htmlspecialchars($address['address_line_2']); ?>">
<label for="city">城市:</label>
<select id="city" name="city" required></select>
<!-- 修改省份选择框,添加onchange事件 -->
<label for="state_province">省份:</label>
<select id="state_province" name="state_province" required onchange="loadCities(this.value)"></select>
<label for="country">国家:</label>
<select id="country" name="country" required onchange="loadProvinces(this.value, '', '<?php echo htmlspecialchars($initialCity); ?>')">
<option value="">选择国家</option>
</select>
<label for="postal_code">邮政编码:</label>
<input type="text" id="postal_code" name="postal_code" value="<?php echo htmlspecialchars($address['postal_code']); ?>" required>
<label for="is_default">设为默认地址:</label>
<input style="margin-left: 50px;" type="checkbox" id="is_default" name="is_default" value="1" <?php echo ($address['is_default'] == 1 ? 'checked' : ''); ?>>
<?php echo "<p class='info'>当前登录用户ID: {$user_id}</p>"; ?>
<?php echo "<p class='info'>当前地址ID: {$address_id}</p>"; ?>
<button type="submit" name="update_address" class="neon-effect">更新</button>
<button class="neon-effect" onclick="redirectToPage()" type="button">返回</button>
</form>
</body>
</html>
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。