I'm using a Joomla site. From the site, I can get the current user id. The user will be either a team manager or an admin (or the page will exit). If the current user is a team manager, the function will return their team number. This part works without issue.
However, if the user is the admin I'd like to have a selection list so the admin can select any team to work on. Once a selection (from the drop down list is made), the function should return that team number.
I've create a short sample:
test1.php
<?php
include("test2.php");
$var = GetValue();
echo $var;
?>
test2.php
<?php
function GetValue() {
// Lookup to get user
if($usertype = "user") {
// some php code here gets the proper team number
return $team_num;
} else {
?>
<select name="potential_user" id="potential_user">
<option value="0">Select User</option>
<option value="1">user1</option>
<option value="2">user2</option>
<option value="3">user3</option>
</select>
<?php
// Once a user is selected above, I need it to be the return value for the function
}
}
I understand that javascript is user-side and php is server-side, but there has to be a way to accomplish this with jQuery, AJAX or something of that nature. I just can't get my mind around it properly.
I have also tried test2.php as:
<script>
function verify() {
var teamNum = document.getElementById('team_num').value;
if(teamNum.length>0) {
document.test.submit();
} else {
alert("You must select a team to continue");
}
}
</script>
<?php
function GetTeamNum() {
if(!isset($_POST['TeamSelect']) ) { $stage=0; }
if(isset($_POST['TeamSelect'])) { $stage=1; }
switch ($stage) {
case 0:
if(file_exists("GetUser.php")) {
include("GetUser.php");
} else {
exit("Cannot continue. Unable to get current user. File missing.");
}
$currentUser = getJoomUser();
if(!$currentUser['username']) { exit("You must be logged in to use this page."); }
if(!file_exists("OpenDB.php")) { exit("Cannot continue. Unable to connect to league database. Missing file"); }
include("OpenDB.php");
$strSQL = "SELECT * FROM chasiv_cbbfl.tbl_defaults;";
$result = mysql_query($strSQL);
if(!$result) { exit("Cannot continue. Unable to connect to get default settings."); }
$rows = mysql_num_rows($result);
switch ($rows) {
case 0:
exit("Unable to find default settings.");
break;
case 1:
$data = mysql_fetch_assoc($result);
$admin = $data["admin"];
break;
default:
exit("Unable to get default settings. Found multiple results!");
break;
}
if(!file_exists("OpenDB.php")) { exit("Cannot continue. Unable to connect to league database. Missing file"); }
include("OpenDB.php");
if($currentUser['username'] != $admin) {
// Not the Admin, so check for current user in League Database
$strSQL = "SELECT * FROM chasiv_cbbfl.tbl_manager_history WHERE jo31_user = " . $currentUser['id'] . " AND dtm_fired IS NULL";
$result = mysql_query($strSQL);
if(!$result) { exit("Unable to execute SQL statement (39)."); }
$rows = mysql_num_rows($result);
switch ($rows) {
case 0:
exit("Unable to find current team for your username.");
break;
case 1:
$data = mysql_fetch_assoc($result);
$teamNum = $data["team_num"];
return $teamNum;
break;
default:
exit("Unable to find current team for your username. Found multiple results!");
break;
}
} else {
// Administrator is logged in, so get the team he/she wishes to work on
$strSQL = "SELECT * FROM chasiv_cbbfl.tbl_team_names
WHERE dtm_end IS NULL
ORDER BY team_name;";
$result = mysql_query($strSQL);
if(!$result) { exit("Unable to execute SQL statement (60)."); }
$rows = mysql_num_rows($result);
If ($rows == 0) { exit("No appropriate team names exist. Page aborted !!"); }
?>
<form name="test" id="test" action="GetTeamNum.php" method="POST">
<input type="hidden" id="team_num" name="team_num" />
<SELECT Name='TeamSelect' onchange="document.getElementById('team_num').value=(this.value)">
<OPTION SELECTED VALUE=''>Pick One</OPTION>
<?php
while($data = mysql_fetch_array($result)) {
unset($sel);
?>
<OPTION VALUE="<?php echo $data['team_num']; ?>"><?php echo $data['team_name']; ?></OPTION>
<?php
}
?>
</SELECT>
<button type="button" id="Submit" name="Submit" onclick="verify();" />
</form>
<?php
}
break;
case 1:
return $_POST['team_num'];
break;
}
}
?>
My thought process here was that the form needs to be posted to get the value into PHP. When the form submits it would go to Case 1 and then have the correct $_POST variable and could pass that back. However, upon completions test1.php did not echo the value to the screen.
Hopefully this helps to get the needed answers. I know I could put the check for admin in test1.php, but I have about 20 forms that will need this test so I'm trying to keep the code all in one place as a function.
if($usertype = "user")should use==You want to compare, not assign.