John Davidson

javascript - PHP - Can't log out and show alert message at the same time

0 comments
Message:


I have a PHP login system where you can delete your account/profile if you'd like to. When the user clicks on the button I want them to be logged out, their account to be deleted and an alert message (JavaScript) saying that their account has been deleted. The problem is that the alert message doesn't appear when I include the logout.php file. If I remove that line of code the alert message appears, but the user will still be logged in. How can I fix this?


Here is my code.


profile.php:


<form class="form" action="delete.php" method="post">
<button type="submit" name="delete" value="delete" onclick="return confirm('Are you sure you want to delete your profile?')">Delete profile</button>
</form>

delete.php:


<?php
session_start();

if (isset($_POST["delete"])) {
require_once 'dbh.inc.php';

$deleteUser = "DELETE FROM users WHERE usersId='".$_SESSION["userid"]."'";

if ($conn->query($deleteUser) === TRUE) {
echo "<script>alert('The profile has been deleted.');</script>";
include 'logout.php';
} else {
echo '<script>alert("Something went wrong: ")</script>';
}

} else {
header("location: ../index.php");
exit();
}

logout.php:


<?php

session_start();
session_unset();
session_destroy();

header("location: ../index.php");
exit();


My Answer:
JavaScript and PHP are two different programming languages that are commonly used for web development. JavaScript is a client-side scripting language that is used to create interactive and dynamic web pages, while PHP is a server-side scripting language that is used to create dynamic web pages and web applications.

While JavaScript and PHP can be used together in web development, they serve different purposes and are typically used in different parts of a web application. JavaScript is used to add interactivity and dynamic behavior to the front-end of a website, while PHP is used to handle server-side processing and interact with databases.

It is common to use JavaScript to make asynchronous requests to a PHP script on the server, which can then process the request and return data back to the client-side JavaScript code. This allows for dynamic and interactive web applications that can communicate with a server to retrieve or update data.

Overall, JavaScript and PHP can work together to create powerful and dynamic web applications, with JavaScript handling client-side interactions and PHP handling server-side processing.

Rate this post

3 of 5 based on 1853 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us