使用ajax运行php函数以更新phpBB模板变量 [英] run php function using ajax to update phpBB template variable

查看:102
本文介绍了使用ajax运行php函数以更新phpBB模板变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

注意:这里有很多细节,因此,如果有人需要这个的精简版,我很乐意进行总结.

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

我试图在我的php文件中运行一个函数,该函数将依次更新模板变量.例如,这是一个这样的功能:

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);
}

我知道此功能有效.我正在尝试通过以下方式在Javascript中访问此功能:

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);
    }
}

我的javascript部分...

The section in my javascript...

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

...是一个块循环,它将遍历PHP函数中设置的块变量vehicle_makes.这在加载页面时有效,因为加载的页面是我试图对其进行Ajax调用的new.php,并且所有PHP在加载时都在该文件中运行.但是,我需要再次运行该函数以更新该块变量,因为它将根据HTML中的选择更改而更改.我不知道这种类型的块循环是否常见.我正在学习它们,因为它们与我在我的网站phpBB上安装的论坛一起使用. (我已经在他们的支持论坛上寻求有关此方面的帮助.).我认为另一种可能的解决方案是返回一个数组,但是为了保持一致性,我想尽可能地坚持使用block变量.

... 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.

这是php中读取$ _POST并调用php函数的代码:

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...
    }
}

这是运行Ajax的javascript:

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);
    }
}

JavaScript将运行,并且ajax将成功.我已经检查了网络"选项卡和控制台"选项卡,并进行了多次测试以确认这一点.似乎没有设置块变量.我正在尝试做的事情甚至有可能吗?我觉得要得到这个答案,我们需要更多地了解phpBB的模板引擎,以及它如何与这些模板变量一起工作.另外,为了澄清起见,我认为术语模板变量"特定于phpBB.这是它们用于在PHP中设置的变量(由HTML和javascript文件访问)的术语.这通过一个名为"template"的phpBB类和一个名为"assign_block_vars"的函数来实现.我不知道该如何工作.

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.

如果有人为phpBB做到了这一点,或者有任何想法,我将不胜感激.

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

推荐答案

认为我发现了问题.在我的PHP的开头,我有一个include语句,以包含PHP文件,该文件包含用于连接数据库的类.在语句$result = $db->sql_query($sql);中,在另一个PHP文件中设置了$db.我并不完全理解,但是由于这个原因,$db不在我的函数get_vehicle_makes()的范围内.我必须在PHP文件中创建一个类,然后使用以下命令将$db作为参数传递给函数:

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);

希望这会有所帮助.

这篇关于使用ajax运行php函数以更新phpBB模板变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆