It is showing this error in console: This page isn’t working. If the problem continues, contact the site owner. HTTP ERROR 405
Header shows this Request URL: http://127.0.0.1:5501/Assignment%202%20-%20Web%20development/submit.php Request Method: POST Status Code: 405 Method Not Allowed Remote Address: 127.0.0.1:5501 Referrer Policy: strict-origin-when-cross-origin
Here is my code that I've been trying
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>XYZ Workshop Registration</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<div class="container">
<h1>XYZ Workshop Registration</h1>
<form id="registration-form" method="POST" action="submit.php">
<div class="form-group">
<label for="fullname">Full Name:</label>
<input type="text" id="fullname" name="fullname" required>
</div>
<div class="form-group">
<label for="email">Email:</label>
<input type="email" id="email" name="email" required>
</div>
<div class="form-group">
<label for="phone">Phone:</label>
<input type="tel" id="phone" name="phone" pattern="[0-9]{10}" required>
</div>
<button type="submit">Register</button>
</form>
</div>
<script src="script.js"></script>
</body>
</html>
submit.php
<?php
// Check if the form is submitted via POST method
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Check if all required fields are set
if(isset($_POST['fullname']) && isset($_POST['email']) && isset($_POST['phone'])) {
// Collect form data
$fullname = $_POST['fullname'];
$email = $_POST['email'];
$phone = $_POST['phone'];
// Perform validation (you can add more validation if needed)
// Connect to your database (replace with your database credentials)
$servername = "localhost";
$username = "your_username";
$password = "your_password";
$dbname = "your_database_name";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Prepare SQL statement to insert data into the database (replace with your table name and column names)
$sql = "INSERT INTO participants (fullname, email, phone) VALUES ('$fullname', '$email', '$phone')";
// Execute SQL statement
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
// Close database connection
$conn->close();
} else {
echo "All fields are required";
}
}
// Check if GET request is made
if ($_SERVER["REQUEST_METHOD"] == "GET") {
// Perform actions based on parameters passed in the URL
// For example, you can retrieve data from the database and return it
if(isset($_GET['id'])) {
// Fetch data based on the ID passed in the URL
$id = $_GET['id'];
// Connect to the database and retrieve data
// Replace this with your own logic to fetch data
$servername = "localhost";
$username = "your_username";
$password = "your_password";
$dbname = "your_database_name";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Prepare SQL statement to fetch data from the database
$sql = "SELECT * FROM participants WHERE id = $id";
// Execute SQL statement
$result = $conn->query($sql);
// Check if data is retrieved
if ($result->num_rows > 0) {
// Output data of each row
while($row = $result->fetch_assoc()) {
echo "Full Name: " . $row["fullname"]. " - Email: " . $row["email"]. " - Phone: " . $row["phone"]. "<br>";
}
} else {
echo "No results found";
}
// Close database connection
$conn->close();
} else {
echo "ID parameter is required";
}
}
?>
Can anyone help to solve this problem? Like where am I wrong?