How can I do same processing with different session value? I am really new in html php ajax and not familiar on how to move the session from pages to another pages. The session value should able to be changed back and forth.
Here is server.php for login. After login, user's family tree succcessfully appeared. $_SESSION['family_id] = users' family id.
if (isset($_POST['login_user'])) {
$username = mysqli_real_escape_string($db, $_POST['username']);
$password = mysqli_real_escape_string($db, $_POST['password']);
if (empty($username)) {
array_push($errors, "Username is required");
}
if (empty($password)) {
array_push($errors, "Password is required");
}
if (count($errors) == 0) {
$password = md5($password);
$query = "SELECT * FROM users WHERE username='$username' AND password='$password'";
$results = mysqli_query($db, $query);
if (mysqli_num_rows($results) == 1) {
while($row = $results->fetch_assoc()) {
$_SESSION['user_id'] = $row["id"];
$_SESSION['family_id'] = $row["family_id"];
}
$_SESSION['username'] = $username;
$_SESSION['success'] = "You are now logged in";
header('location: index.php');
}else {
array_push($errors, "Wrong username/password combination");
}
}
}
Then I add search features at server.php. After user search for name John, the John's family tree successfully appeared. $_SESSION['family_id]= John's family id.
if (isset($_POST['find_user'])) {
if (!empty($_REQUEST['username'])) {
$username = mysqli_real_escape_string($db, $_REQUEST['username']);
$query = "SELECT * FROM users WHERE username LIKE '%".$username."%'";
$results = mysqli_query($db, $query);
while ($row = $results->fetch_assoc()){
$_SESSION['family_id'] = $row["family_id"];
}
}
}
And now is the problem. I want the user can go back to view his family tree. So at 'index.php' I add section "Your family tree" at navigation bar using AJAX. But I failed to view the user's family tree.
<div>
<li class="p-1 sm:p-0"><a id="clickme">Your family Tree</a>
</li>
</div>
<script>
$("#clickme").click(function() {
var info = "close";
$.ajax({
type:'POST',
url:'index.php',
data: {info:info}
});
});
</script>
While at server.php
if (isset($_POST['info'])) {
$user_id = $_SESSION['user_id'];
$query = "SELECT * FROM users WHERE id='$user_id'";
$results = mysqli_query($db, $query);
while($row = $results->fetch_assoc()) {
$_SESSION['family_id'] = $row["family_id"];
}
header('location: index.php');
}
I would be grateful if you can provide any link for further reading.