代码拉取完成,页面将自动刷新
<?php
session_start();
function checkUserLoggedIn() {
// 假设您有一个会话变量 `$_SESSION['user_id']` 存储已登录用户的ID
if (isset($_SESSION['id'])) {
return (int)$_SESSION['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 ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['add_address'])) {
$address_line_1 = $_POST['address_line_1'];
$address_line_2 = $_POST['address_line_2'];
$city = $_POST['city'];
$state_province = $_POST['state_province'];
$country = $_POST['country'];
$postal_code = $_POST['postal_code'];
$is_default = isset($_POST['is_default']) ? 1 : 0;
$stmt = $pdo->prepare("
INSERT INTO user_addresses (user_id, address_line_1, address_line_2, city, state_province, country, postal_code, is_default)
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
");
$stmt->execute([$user_id, $address_line_1, $address_line_2, $city, $state_province, $country, $postal_code, $is_default]);
if ($stmt->rowCount() > 0) {
echo "<p class='success'>新地址已成功添加!</p>";
} else {
echo "<p class='error'>添加地址时发生错误,请稍后重试。</p>";
}
}
echo'<div class="fornow">';
echo '<table class="animate__animated animate__fadeIn">
<h2 class="h22">当前收货地址</h2>
<thead>
<tr>
<th>地址1</th>
<th>地址2</th>
<th>城市</th>
<th>省份/州</th>
<th>国家</th>
<th>邮政编码</th>
<th>是否默认</th>
<th>操作</th>
</tr>
</thead>
<tbody>';
$stmt = $pdo->prepare("SELECT * FROM user_addresses WHERE user_id = ?");
$stmt->execute([$user_id]);
while ($address = $stmt->fetch(PDO::FETCH_ASSOC)) {
echo "
<tr>
<td>{$address['address_line_1']}</td>
<td>{$address['address_line_2']}</td>
<td>{$address['city']}</td>
<td>{$address['state_province']}</td>
<td>{$address['country']}</td>
<td>{$address['postal_code']}</td>
<td>" . ($address['is_default'] == 1 ? '是' : '否') . "</td>
<td>
<a href='edit_address.php?address_id={$address['id']}'>编辑</a> /
<a href='delete_address.php?address_id={$address['id']}' data-address-id='{$address['id']}' class='delete-link'>删除</a>
</td>
</tr>
";
}
echo '</tbody>
</table>';
echo'</div>';
// 关闭数据库连接
$pdo = null;
?>
<script type="text/javascript">
function redirectToPage() {
window.location.href = 'PersonalCenter.php';
}
</script>
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="icon" href="img/favicon.ico" type="image/x-icon">
<title>添加收获地址 | Cyberpunk2077未来车辆</title>
<link
rel="stylesheet"
href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/4.1.1/animate.min.css"
/>
<style>
tr th{
color: white;
font-weight: bold;
border-bottom: 1px solid #FEF629;
padding-bottom: 10px;
}
tr td{
color: white;
padding-left: 10px;
border-bottom: 2px solid #01E0EF;
padding-bottom: 5px;
padding-top: 5px;
letter-spacing: 3px;
}
tr td a{
color: #FEF629;
text-decoration: none;
transition:ease 0.3s;
}
tr td a:hover{
color: #01E0EF;
transition:ease 0.3s;
}
select {
margin-bottom: 10px;
}
body {
font-family: Arial, sans-serif;
background-color: #f2f2f2;
height: 100%;
width: 100%;
background-size: cover;
position: absolute;
overflow: hidden;
}
form {
padding: 3rem;
border-radius: 8px;
box-shadow: 0 0 100px red;
width: 400px;
max-width: 400px;
margin-left: 1150px;
height: 650px;
border: 1px solid blue;
background-color: rgba(0,0,0,0.4);
margin-top: -400px;
}
label {
display: block;
margin-bottom: 1rem;
}
.border1 {
width: 300px;
padding: 0.5rem;
margin-bottom: 1rem;
border: none;
border-radius: 3px;
box-sizing: border-box;
margin-left: 40px;
}
.border2 {
width: 300px;
padding: 0.5rem;
margin-bottom: 1rem;
border: none;
border-radius: 3px;
box-sizing: border-box;
margin-left: 40px;
}
.border3 {
width: 300px;
padding: 0.5rem;
margin-bottom: 1rem;
border: none;
border-radius: 3px;
box-sizing: border-box;
margin-left: 40px;
}
.border4 {
font-style: italic;
font-size: 15px;
font-family: impact;
text-shadow: 0 0 3vw #F4BD0A;
animation: neon 2s ease-in-out infinite;
color: #FEF629;
width: 1000px;
}
.border5 {
font-style: italic;
font-size: 15px;
font-family: impact;
text-shadow: 0 0 3vw #F4BD0A;
animation: neon 2s ease-in-out infinite;
}
.labe1 {
color: black;
letter-spacing: 10px;
font-weight: bolder;
font-style: italic;
font-family: ;
text-shadow: 1px 1px #00F0FF;
margin-left: 40px;
}
.container {
max-width: 800px;
margin: 0 auto;
padding-left: 20px;
padding-right: 20px;
padding-top: 20px;
padding-bottom: 20px;
border-radius: 10px;
}
.fornow
{
width: 700px;
height: 400px;
margin-top: 110px;
margin-left: 300px;
}
.h21 {
text-align: center;
font-size: 2rem;
color: #FEF629;
text-shadow: -2px 2px 2px #00F0FF;
margin-top: -20px;
}
.h22 {
font-size: 2rem;
color: #FEF629;
text-shadow: -2px 2px 2px #00F0FF;
margin-left: 240px;
}
.success {
color: #00ff00;
border: 1px solid #00ff00;
background-color: rgba(0, 255, 0, 0.1);
padding: 10px;
border-radius: 5px;
margin-bottom: 10px;
}
.error {
color: #ff0000;
border: 1px solid #ff0000;
background-color: rgba(255, 0, 0, 0.1);
padding: 10px;
border-radius: 5px;
margin-bottom: 10px;
}
table {
width: 700px;
border-collapse: collapse;
}
.bjimg {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
z-index: -10;
object-fit: cover;
}
select{
margin-left: 40px;
width: 300px;
height: 30px;
}
.is_default{
margin-left: 40px
;
}
.add_address,
button::after {
width: 100px;
height: 50px;
font-size: 20px;
background: linear-gradient(45deg, transparent 5%, #FF013C 5%);
border: 0;
color: #fff;
letter-spacing: 2px;
line-height: 30px;
box-shadow: 6px 0px 0px #00E6F6;
outline: transparent;
position: relative;
margin-left: 40px;
}
.add_address::after {
--slice-0: inset(50% 50% 50% 50%);
--slice-1: inset(80% -6px 0 0);
--slice-2: inset(50% -6px 30% 0);
--slice-3: inset(10% -6px 85% 0);
--slice-4: inset(40% -6px 43% 0);
--slice-5: inset(80% -6px 5% 0);
content: '添加';
display: block;
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
background: linear-gradient(45deg, transparent 3%, #00E6F6 3%, #00E6F6 5%, #FF013C 5%);
text-shadow: -3px -3px 0px #F8F005, 3px 3px 0px #00E6F6;
clip-path: var(--slice-0);
font-style: italic;
margin-left: 0px;
}
.add_address:hover::after {
animation: 1s glitch;
animation-timing-function: steps(2, end);
}
.goback,
button::after {
width: 100px;
height: 50px;
font-size: 20px;
background: linear-gradient(45deg, transparent 5%, #FF013C 5%);
border: 0;
color: #fff;
letter-spacing: 2px;
line-height: 30px;
box-shadow: 6px 0px 0px #00E6F6;
outline: transparent;
position: relative;
margin-left: 95px;
}
.goback::after {
--slice-0: inset(50% 50% 50% 50%);
--slice-1: inset(80% -6px 0 0);
--slice-2: inset(50% -6px 30% 0);
--slice-3: inset(10% -6px 85% 0);
--slice-4: inset(40% -6px 43% 0);
--slice-5: inset(80% -6px 5% 0);
content: '返回';
display: block;
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
background: linear-gradient(45deg, transparent 3%, #00E6F6 3%, #00E6F6 5%, #FF013C 5%);
text-shadow: -3px -3px 0px #F8F005, 3px 3px 0px #00E6F6;
clip-path: var(--slice-0);
font-style: italic;
margin-left: 0px;
}
.goback:hover::after {
animation: 1s glitch;
animation-timing-function: steps(2, end);
}
@keyframes glitch {
0% {
clip-path: var(--slice-1);
transform: translate(-20px, -10px);
}
10% {
clip-path: var(--slice-3);
transform: translate(10px, 10px);
}
20% {
clip-path: var(--slice-1);
transform: translate(-10px, 10px);
}
30% {
clip-path: var(--slice-3);
transform: translate(0px, 5px);
}
40% {
clip-path: var(--slice-2);
transform: translate(-5px, 0px);
}
50% {
clip-path: var(--slice-3);
transform: translate(5px, 0px);
}
60% {
clip-path: var(--slice-4);
transform: translate(5px, 10px);
}
70% {
clip-path: var(--slice-2);
transform: translate(-10px, 10px);
}
80% {
clip-path: var(--slice-5);
transform: translate(20px, -10px);
}
90% {
clip-path: var(--slice-1);
transform: translate(-10px, 0px);
}
100% {
clip-path: var(--slice-1);
transform: translate(0);
}
}
</style>
</head>
<body>
<video src="video/Cyberpunk_2077_1080.mp4" class="bjimg" autoplay loop muted=></video>
<form method="POST" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" enctype="multipart/form-data" class="animate__animated animate__fadeIn" >
<div class="container" >
<h2 class="h21">添加收货地址</h2>
<label for="address_line_1" class="labe1">地址1:</label>
<input type="text" id="address_line_1" name="address_line_1" class="border1">
<label for="address_line_2"class="labe1" >地址2:</label>
<input type="text" id="address_line_2" name="address_line_2" class="border2" placeholder="可为空">
<label for="address_line_2"class="labe1" >国家:</label>
<select id="country" name="country" required onchange="loadProvinces(this.value)">
<option value="">选择国家</option>
</select>
<label for="address_line_2"class="labe1" >省份:</label>
<select id="state_province" name="state_province" required onchange="loadCities(this.value)">
<option value="">选择省份</option>
</select>
<label for="address_line_2"class="labe1" >城市:</label>
<select id="city" name="city" required></select>
<label for="address_line_2"class="labe1" >邮政编码:</label>
<input type="text" id="postal_code" name="postal_code" required class="border3">
<label for="address_line_2"class="labe1" >是否设为默认地址:</label>
<input type="checkbox" id="is_default" name="is_default" value="1" class="is_default"><br/><br/>
<button type="submit" name="add_address" class="add_address">添加</button>
<button class="goback" onclick="redirectToPage()" type="button">返回</button>
</div>
</form>
<script>
let cachedLocationsData;
function loadLocations(callback) {
var xhr = new XMLHttpRequest();
xhr.open('GET', 'locations.json', true);
xhr.onload = function() {
if (xhr.status === 200) {
cachedLocationsData = JSON.parse(xhr.responseText);
callback(cachedLocationsData);
} else {
console.error('Failed to load locations data.');
}
};
xhr.send();
}
function loadProvinces(country) {
console.log('Loading provinces for country:', country);
var provinceSelect = document.getElementById('state_province');
provinceSelect.innerHTML = '<option value="">选择省份</option>';
if (country !== '') {
if (!cachedLocationsData) {
loadLocations(data => {
var provinces = data.countries.find(c => c.name === country).provinces;
console.log('Found provinces:', provinces);
provinces.forEach(p => {
var option = document.createElement('option');
option.value = p.name;
option.text = p.name;
provinceSelect.appendChild(option);
});
loadCities(provinceSelect.value);
});
} else {
var provinces = cachedLocationsData.countries.find(c => c.name === country).provinces;
console.log('Found provinces:', provinces);
provinces.forEach(p => {
var option = document.createElement('option');
option.value = p.name;
option.text = p.name;
provinceSelect.appendChild(option);
});
loadCities(provinceSelect.value);
}
}
}
function getCitiesForProvince(data, province) {
return data.countries.flatMap(country => {
const provinces = country.provinces;
if (provinces && provinces.some(p => p.name === province)) {
return provinces.find(p => p.name === province).cities.map(c => c.name);
}
return [];
});
}
function loadCities(province) {
console.log('Loading cities for province:', province);
var citySelect = document.getElementById('city');
citySelect.innerHTML = '<option value="">选择城市</option>';
if (province !== '' && province !== '选择省份') {
if (!cachedLocationsData) {
loadLocations(data => {
var cities = getCitiesForProvince(data, province);
console.log('Found cities:', cities);
cities.forEach(c => {
var option = document.createElement('option');
option.value = c;
option.text = c;
citySelect.appendChild(option);
});
});
} else {
var cities = getCitiesForProvince(cachedLocationsData, province);
console.log('Found cities:', cities);
cities.forEach(c => {
var option = document.createElement('option');
option.value = c;
option.text = c;
citySelect.appendChild(option);
});
}
}
}
// 加载初始国家选项
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);
});
});
document.addEventListener('DOMContentLoaded', function() {
// 为所有删除链接添加事件监听器
var deleteLinks = document.querySelectorAll('a[href^="delete_address.php"]');
deleteLinks.forEach(function(link) {
link.addEventListener('click', function(event) {
event.preventDefault(); // 阻止链接的默认跳转行为
var confirmed = confirm("确定要删除此地址吗?");
if (confirmed) {
var addressId = this.getAttribute('data-address-id');
deleteAddress(addressId);
}
});
});
// AJAX 删除地址函数
function deleteAddress(addressId) {
var xhr = new XMLHttpRequest();
xhr.open('GET', 'delete_address.php?address_id=' + encodeURIComponent(addressId), true);
xhr.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
xhr.onreadystatechange = function () {
if (xhr.readyState === 4 && xhr.status === 200) {
var response = JSON.parse(xhr.responseText);
if (response.success) {
alert('地址已成功删除');
// 可以在这里使用response数据来更新页面,如重新加载地址列表
location.reload(); // 简单地刷新页面以显示更新后的地址列表
} else {
alert(response.message);
}
}
};
xhr.send();
}
});
</script>
</body>
</html>
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。