My code works perfectly fine when I use it locally and can access the server (using XAMPP).
However, when I run it through Cordova I can not display the username. The username is saved as a $_SESSION variable in the login.php file when the user logs in, but gets the error message Warning: Undefined array key “username”.
The postings of the web application are shown, so I know I can access the server when using Cordova as well.
In the HTML file, the displayUserName() function is called onload which sends a request to displayName.php.
The displayAllPosts() function works.
Any help would be appreciated.
<?php
session_start();
// Establish connection to database
include("connection.php");
header('Access-Control-Allow-Origin: *');
//header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS');
//header('Access-Control-Allow-Headers: Content-Type');
// Setting the vairables for the email and password.
$email = $_POST['email'];
$password = $_POST['password'];
//SQL query for retrieving the username and userID where email is
//current email and password is current password.
$sql_user = "SELECT username, user_id FROM users WHERE email = '$email' AND password = '$password'";
// Store the result from the sql query
$result_user = mysqli_query($link, $sql_user) or die (mysqli_connect($link));
// Check if there is data returned.
// If num of rows returned is 0, then there is no registered user with
// the provided email and password
if ((mysqli_num_rows($result_user) < 1)){
echo 1;
}
// Otherwise the username and password has been found in the db.
else{
//Saving the result as an associative array called row.
$row = mysqli_fetch_array($result_user);
//Setting the session variable for username and user id.
$_SESSION['username'] = $row['username'];
$_SESSION['user_id'] = $row['user_id'];
echo 2;
}
// Close the connection to the db
$conn->close();
?>
<?php
session_start();
// Establish connection to database
include("connection.php");
header('Access-Control-Allow-Origin: *');
//header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS');
//header('Access-Control-Allow-Headers: Content-Type');
// Session vaiable for username when logged in.
echo $_SESSION['username'];
?>
function displayUserName() {
$.ajax({
url: 'http://MY_IP_ADDRESS/test/server/displayName.php',
type: 'GET',
data: {},
success: function (userdata) {
// Not found in db. Provide feedback to user.
if (!userdata) {
document.getElementById("displayname").innerHTML = "No text";
}
//Successfully found in db and username will be displayed.
if (userdata) {
document.getElementById("displayname").innerHTML = "Inloggad: " + userdata;
}
}
});
};
function displayAllPosts() {
$.ajax({
url: 'http://MY_IP_ADDRESS/test/server/displayAllPosts.php',
type: 'GET',
data: {},
dataType: "json",
success: function (data) {
//Check if there is data.
if (!data) {
document.getElementById("displayAllPosts").innerHTML = "no data.";
}
else {
//Create a constant to determine in which element the postings
//should be displayed. This element will be the parent element.
const allPosts = document.getElementById('displayAllPosts');
<body onload="displayUserName(); displayAllPosts()">
<!-- Here is the logo -->
<div class="container pt-3 pb-3">
<div class="row">
<div class="justify-content-center">
<img src="img/hth.png" alt="Here to help logo" width="300" height="100">
</div>
</div>
</div>
<!-- Display the username that is logged in-->
<div class="container pt-3 pb-1">
<div class="row">
<div class="col d-flex justify-content-end" id="displayname">
</div>
</div>
</div>
<!-- Display the name of the section-->
<div class="container pt-1 pb-3">
<div class="row">
<div class="col d-flex justify-content-center">
<h1>Inlägg</h1>
</div>
</div>
</div>
<!-- Div to display all posts-->
<div class="container pt-1 pb-3">
<div class="row">
<div class="event-wrapper" id="displayAllPosts">
</div>
</div>
</div>