This topic is locked

Extra Login field

2/22/2019 10:05:26 AM
PHPRunner General questions
G
Grdimitris author

I want employees to login and have the choice to choose which branch they are in. The application proposes the branch owned but can choose the branch located at the time.

So I did the following

I modified the Login page to include the branch owned by the employee using the application by adding the following
<tr>

<TD class="r-align-right r-ori-vert col-md-4" data-single data-cellId="grid_c1" data-pageid="{$pageid}">

<DIV edit-form-cell class="clearfix">

<LABEL data-itemtype="username_label" data-itemid="username_label" {$item_ypokatastima_label} data-pageid="{$pageid}" class="r-edit-label control-label" for="username">

{BEGIN ypokatastima_label}

branch:

{END ypokatastima_label}

</label>

</div>

</td>

<TD class="r-ori-vert col-md-6" data-single data-cellId="grid_c2" data-pageid="{$pageid}">

<DIV edit-form-cell class="clearfix">

<DIV class="r-edit-field" data-itemtype="username" data-itemid="ypokatastima" {$item_ypokatastima} data-pageid="{$pageid}">

<SELECT type="text" class="form-control" name="ypokatastima" {$ypokatastima_attrs} id="ypokatastima">

</select>

</div>

</div>

</td>

<TD class="col-md-2">

</td>

</tr>

In the tab events on the Login page kai sto javascript on load I have the following code
// write or autoupdate username

$("#username").on('input', function() {

var onomaxristi=document.getElementById("username");

var userid=onomaxristi.value;

//Ενημέρωση Λίστας select

var selectList = document.getElementById("ypokatastima");

$("#ypokatastima").find('option')

.remove();

//create and fill select Tag for branch names stored in table ypokatastimata ( selected is the coresponded branch field from users table)
$.ajax({
url: 'get_ypokatastimata.php',

type: 'get',

dataType:'json',

data: {id: userid},
success: function(response)

{
var sel=response.perigrafi;

for (var i = 0; i < Object.keys(response).length; i++) {

var option = document.createElement("option");

option.setAttribute("value", response[i].perigrafi);

option.text = response[i].perigrafi;

if(response[i].perigrafi==sel){ option.setAttribute("selected", true);} // show in select Tag the branch he works

selectList.appendChild(option);

}
}

});
});

// send branch name to $_SESSION variable "ypokatastima"
$("#ypokatastima").on('input', function() {

var selecttimi = document.getElementById("ypokatastima").value;

$.ajax({
url: 'session_ypokatastimata.php',

type: 'get',

dataType:'json',

data: {id: selecttimi},
success: function(response)

{
}

});
});

The php files for AJAX calls are
// get_ypokatastimata.php
<?php

header("Content-Type: application/xml; charset=utf-8"); to read the right charset of Mysql Table

mb_internal_encoding('UTF-8'); // For unknown reason to respond text not as ??????? it also needs this

$con = mysqli_connect('localhost','agri9335_grdim','skiathos!!17','agri9335_grtest');

if (!$con) {

die('Could not connect: ' . mysqli_error($con));

}

mysqli_set_charset($con,"utf8");

mysqli_select_db($con,"ajax_connect");
$xeiristis = $_GET['id'];
$sql1 = "SELECT ypokatasthma FROM grigoriadis_users WHERE user_name= '$xeiristis'";

$apotelesma = mysqli_query($con,$sql1);
$data1=mysqli_fetch_array($apotelesma);
$result = array(

'perigrafi' => $data1["ypokatasthma"] // object result.perigrafi the branch where employee works

);

$sql = "SELECT
FROM ypokatastimata";

$apotelesma = mysqli_query($con,$sql);

//$rs = CustomQuery($sql);

//$result[] = array();//ergasia

while ($data = mysqli_fetch_array($apotelesma)) {

$result[] = array(

'perigrafi' => $data['Onomasia'] // all branch names

);

}

echo json_encode($result);

mysqli_close($con);

?>
and the second PHP file were i store $_SESSION variable
//session_ypokatastimata.php
<?php

include("include/dbcommon.php");

if (!isset($_SESSION)) session_start();
$_SESSION["ypokatasthma"] = $_GET['id'];

?>

*

Everything works well except the last action that does not modify the SESSION variable

I have read an older post that suggests that I must write session_start(); at the beginning of external PHP file where i assign SESSION variable

But nothing.

Please help me and indicate to me were i am wrong

admin 2/22/2019

If you do include("include/dbcommon.php"); in external file it will start the session for you. No need to do anything else.

G
Grdimitris author 2/23/2019



If you do include("include/dbcommon.php"); in external file it will start the session for you. No need to do anything else.


I placed include("include/dbcommon.php"); in external file but still session variable is not assigned, keeps value from login before process.
The only way i found is storing in users table in a field named last_branch the value i want to assign to session variable and in after login function to assign this field to session variable.
But i thing this is not the proper and best way. Maybe if we could add extra custom parameters to functions is an easy way

for example in function AfterSuccessfulLogin($username, $password, $data, $pageObject) except $username and $password one or more extra parameters if someone wants extra fields in login page (a json object is excellent idea)