0

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.

1
  • 2
    Sidenote: You have a slight syntax error. if($usertype = "user") should use == You want to compare, not assign. Commented Apr 2, 2014 at 0:26

2 Answers 2

1

In addition to chitowns answer being correct (and obviously from a cool city), there are done Joomla specifics which will their up roadblocks.

1) Loading jQuery library: Joomla provides a nice method to load the library. Include at the top of any view php files with jQuery code.

JHtml::_('jquery.framework');

2) Mootools and jQuery don't play nice together: although transitioning away from it, Joomla still uses Mootools heavily, so be sure to wrap all your jQuery code in an IIFE:

(function ($) {

     // place jQuery code here

})(jQuery);

3) Are running the server side PHP code through Joomla's MVC? Although it is much simpler initially to just process outside, you will lose access to a host of security and utility features built into the core. Form fields being sanitized, easy form tokens and access to core data objects to easily pull user info and leverage built in ACL. I can expound on this if you like and l can provide more code from the extension you are working on and intent.

Sign up to request clarification or add additional context in comments.

Comments

0

Here is a simple example that takes the value on a <select> change in jQuery and AJAX

 $("#potential_user").change(function(){
            $.ajax({
        url: "test1.php",
        data: {value: $(potential_user).val()},
        type: "post",
        success: function(msg){
        alert(msg);
        }
    });
  });

In your PHP file test1.php the variable will be available as $_POST['value'] and anything you echo in test1 will be alerted on the success

3 Comments

This won't work. Joomla and Ajax are different to normal PHP and Ajax
The other challenge that I have is this is an include file. If I use a form, I don't believe it will post back and then pass the returned value back to the original function. I tried simply creating a 2-stage form and return the value of $_POST['potential_user'] in stage 2 (after posting) and it did not seem to work.
Well what does your include file look like?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.