在代码末尾,我使用 php 中的 header 函数重定向到另一个文件。我在项目的其他地方使用了它,它似乎有效。但在这里不起作用。
<?php
if ($_SERVER["REQUEST_METHOD"] === "POST") {
// Read the raw POST data
$postData = file_get_contents('php://input');
// Convert the raw POST data to a PHP array
$formData = json_decode($postData, true);
$db = new PDO('sqlite:../database.db');
$hashedPassword = password_hash($formData['password'], PASSWORD_DEFAULT);
$uniqueId = uniqid();
$createdAt = date('Y-m-d H:i:s');
$query = "INSERT INTO users (id, username, email, password, created_at)
VALUES (:id, :username, :email, :password, :created_at)";
$statement = $db->prepare($query);
// Bind parameters and execute the query
$statement->bindParam(':id', $uniqueId);
$statement->bindParam(':username', $formData['username']);
$statement->bindParam(':email', $formData['email']);
$statement->bindParam(':password', $hashedPassword);
$statement->bindParam(':created_at', $createdAt);
$statement->execute();
// Set session variables to log in the user
session_start();
$_SESSION['user_id'] = $uniqueId;
$_SESSION['username'] = $formData['username'];
// Redirect the user to index.php after successful account creation
header('Location: ../html/index.php');
exit();
}
?>
这是我获取 php 的 javascript
document.getElementById('signup-button-middle').addEventListener('click', handleSignUp);
function handleSignUp() {
// Get the data from the textareas
const username = document.getElementById('username').value;
const email = document.getElementById('email').value;
const password = document.getElementById('password').value;
// Create an object with the data to be sent to the PHP file
const formData = {
username: username,
email: email,
password: password
};
// Make a POST request using fetch to send the data to the PHP file in the "PHP" folder
fetch('../PHP/signup_handler.php', { // Modify the URL to include the "PHP" folder
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(formData)
})
.then(response => response.json())
.then(data => {
// Handle the response from the PHP file (if needed)
console.log(data);
// You can perform additional actions based on the response from the PHP file here.
})
.catch(error => {
// Handle errors, if any
console.error('Error:', error);
});
}
当我使用 JavaScript fetch 执行此代码时,我尝试在浏览器中显示的 html 不会重定向到其他页面,而是在检查页面后显示在网络选项卡中
您可以使用 javascript 中的
window.location.replace("../html/index.php");
在收到 PHP 响应后将用户重定向到另一个页面:
php代码
<?php
if ($_SERVER["REQUEST_METHOD"] === "POST") {
// Read the raw POST data
$postData = file_get_contents('php://input');
// Convert the raw POST data to a PHP array
$formData = json_decode($postData, true);
$db = new PDO('sqlite:../database.db');
$hashedPassword = password_hash($formData['password'], PASSWORD_DEFAULT);
$uniqueId = uniqid();
$createdAt = date('Y-m-d H:i:s');
$query = "INSERT INTO users (id, username, email, password, created_at)
VALUES (:id, :username, :email, :password, :created_at)";
$statement = $db->prepare($query);
// Bind parameters and execute the query
$statement->bindParam(':id', $uniqueId);
$statement->bindParam(':username', $formData['username']);
$statement->bindParam(':email', $formData['email']);
$statement->bindParam(':password', $hashedPassword);
$statement->bindParam(':created_at', $createdAt);
$statement->execute();
// Set session variables to log in the user
session_start();
$_SESSION['user_id'] = $uniqueId;
$_SESSION['username'] = $formData['username'];
}
?>
javascript代码
document.getElementById('signup-button-middle').addEventListener('click', handleSignUp);
function handleSignUp() {
// Get the data from the textareas
const username = document.getElementById('username').value;
const email = document.getElementById('email').value;
const password = document.getElementById('password').value
// Create an object with the data to be sent to the PHP file
const formData = {
username: username,
email: email,
password: password
};
// Make a POST request using fetch to send the data to the PHP file in the "PHP" folder
fetch('../PHP/signup_handler.php', { // Modify the URL to include the "PHP" folder
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(formData)
})
.then(response => response.json())
.then(data => {
// Handle the response from the PHP file (if needed)
console.log(data);
window.location.replace("../html/index.php"); //change path if not correct
// You can perform additional actions based on the response from the PHP file here.
})
.catch(error => {
// Handle errors, if any
console.error('Error:', error);
});
}