run php function using ajax to update phpBB templa

2019-08-17 01:41发布

NOTE: There are a lot of details here, so if anyone needs a condensed version of this, I'm happy to summarize.

I am trying to run a function in my php file, that will in turn, update a template variable. As an example, here is one such function:

function get_vehicle_makes()
{
$sql = 'SELECT DISTINCT make FROM phpbb_vehicles
        WHERE year = ' . $select_vehicle_year;

$result = $db->sql_query($sql);

while($row = $db->sql_fetchrow($result))
{
    $template->assign_block_vars('vehicle_makes', array(
        'MAKE'    => $row['make'],
    ));
}
$db->sql_freeresult($result);
}

I know that this function works. I am trying to access this function in my Javascript with:

function updateMakes(pageLoaded) {
    var yearSelect = document.getElementById("vehicle_year");
    var makeSelect = document.getElementById("vehicle_make");
    var modelSelect = document.getElementById("vehicle_model");

    $('#vehicle_make').html('');

    $.ajax({ url: '/posting.php',
            data: {action: 'get_vehicle_makes'},
            type: 'post',
            success:function(result)//we got the response
            {
            alert(result);
            },
            error:function(exception){alert('Exception:'+exception);}
    });
    <!-- BEGIN vehicle_makes -->
        var option = document.createElement("option");
        option.text = ('{vehicle_makes.MAKE}');
        makeSelect.add(option);
    <!-- END vehicle_makes -->

    if(pageLoaded){
        makeSelect.value='{VEHICLE_MAKE}{DRAFT_VEHICLE_MAKE}';
        updateModels(true);
    }else{
        makeSelect.selectedIndex = -1;
        updateModels(false);
    }
}

The section in my javascript...

<!-- BEGIN vehicle_makes -->
        var option = document.createElement("option");
        option.text = ('{vehicle_makes.MAKE}');
        makeSelect.add(option);
<!-- END vehicle_makes -->

... is a block loop and will loop through the block variable, vehicle_makes, set in the PHP function. This works upon loading the page because the page that loads, is the new.php that I'm trying to do an Ajax call to, and all of the PHP runs in that file upon loading. However, I need the function to run again, to update that block variable, since it will change based on a selection change in the HTML. I don't know if this type of block loop is common. I'm learning about them since they are used with a forum I've installed on my site, phpBB. (I've looked in their support forums for help on this.). I think another possible solution would be to return an array, but I would like to stick to the block variable if possible for the sake of consistency.

This is the bit of code in the php that reads the $_POST, and call the php function:

if(isset($_POST['action']) && !empty($_POST['action'])) {
    $action = $_POST['action'];

    //Get vehicle vars - $select_vehicle_model is used right now, but what the heck.
    $select_vehicle_year = utf8_normalize_nfc(request_var('vehicle_year', '', true));
    $select_vehicle_make = utf8_normalize_nfc(request_var('vehicle_make', '', true));
    $select_vehicle_model = utf8_normalize_nfc(request_var('vehicle_model', '', true));

    switch($action) {
    case 'get_vehicle_makes' :
        get_vehicle_makes();
        break;
    case 'get_vehicle_models' :
        get_vehicle_models();
        break;
    // ...etc...
    }
}

And this is the javascript to run the Ajax:

function updateMakes(pageLoaded) {
    var yearSelect = document.getElementById("vehicle_year");
    var makeSelect = document.getElementById("vehicle_make");
    var modelSelect = document.getElementById("vehicle_model");

    $('#vehicle_make').html('');

    $.ajax({ url: '/posting.php',
            data: {action: 'get_vehicle_makes'},
            type: 'post',
            success:function(result)//we got the response
            {
            alert(result);
            },
            error:function(exception){alert('Exception:'+exception);}
    });
    <!-- BEGIN vehicle_makes -->
        var option = document.createElement("option");
        option.text = ('{vehicle_makes.MAKE}');
        makeSelect.add(option);
    <!-- END vehicle_makes -->

    if(pageLoaded){
        makeSelect.value='{VEHICLE_MAKE}{DRAFT_VEHICLE_MAKE}';
        updateModels(true);
    }else{
        makeSelect.selectedIndex = -1;
        updateModels(false);
    }
}

The javascript will run, and the ajax will be successful. I've checked the network tab and console tab, and have done multiple tests to confirm that. It appears that the block variable is not being set. Is what I'm trying to do even possible? I have a feeling that to get this answer, we'll need to know more about phpBB's template engine, and how it works with these template variable. Also, just to clarify, I think the term 'template variable' is specific to phpBB. It's the term they use for variables set in PHP, to be accessed by the HTML, and javascript files. This works through a phpBB class called 'template', and a function called 'assign_block_vars'. I don't know exactly how that work.

If anyone has done this for phpBB, or has any ideas, I would appreciate it.

1条回答
冷血范
2楼-- · 2019-08-17 01:53

Think I found the problem. At the beginning of my PHP, I have an include statement to include the PHP file containing the class for connecting to the database. In the statement $result = $db->sql_query($sql);, $db is set in this other PHP file. I don't entirely understand, but because of that, $db was outside of the scope of my function get_vehicle_makes(). I had to create a class inside my PHP file, and pass $db as a parameter to the function using:

class vehicle {

    public function __construct($db)
    {
        $this->db = $db;
    }

function get_vehicle_makes()
{
    $sql = 'SELECT make FROM phpbb_vehicles
            WHERE year = ' . $select_vehicle_year;  
    $result = $this->db->sql_query($sql);

Hope this helps.

查看更多
登录 后发表回答