使用AJAX和PHP的Zend_Config_Ini

时间:2012-02-11 02:15:34

标签: php html ajax zend-framework

我目前正在创建自己的CMS,我有一个难题(我认为是这样)而我在谷歌上找不到任何东西......

我正在尝试使用AJAX和PHP创建edit_settings.php,将Zend_Config_Ini包含在其中,因为我正在使用它进行config.ini解析和编写。

PHP代码的基础(我的数组不存在,正常):

<?php
$config = new Zend_Config_Ini('config.ini',null,array('skipExtends' => true, 'allowModifications' => true));

// Write the config file
$writer = new Zend_Config_Writer_Ini(array('config' => $config,'filename' => 'config.ini'));
$writer->write();
?>

例如:我点击“编辑”,红边区域成为输入字段,我可以在不更改页面的情况下编辑它,我只需要点击“保存”后将配置保存在config.ini中文件。

CMS Admin

我知道这需要AJAX,PHP和Zend_Config_Ini,但我不知道如何将它们链接在一起......

问题:我该怎么做?

2 个答案:

答案 0 :(得分:1)

我不会用整页刷新的东西。它为控制器增加了额外的逻辑。逻辑可以很容易地在专门的ajax控制器中。而jQuery的AJAX很酷。而AJAX非常适合编辑内容。

我将做出以下假设:

  • 你使用jQuery

  • 您知道有效的JSON字符串成为jQuery中的JS对象

  • 你有一个带有“settingsEditAction”的AjaxController

  • 你知道这是我的处理你的问题,而不是强加任何风格 思考甚至编码;这就是会这样做的,所以这很糟糕 或者好。

  • 你知道我已经用np ++写了这篇文章,但是你不会因为缺少冒号等而感到害怕

在AjaxController中:

public function settingsEditAction()
{
    $json = array('success' => false);

    $inputName = $this->_getParam("inputName");
    $inputVal  = $this->_getParam("inputVal");

    // all OK?
    if (!$this->_request->isXmlHtmlRequest() || 
        !$this->_request->isPost() ||
        is_null($inputName) ||
        is_null($inputVal)) 
    {
        $this->_helper->json($json);
    }


    $iniWriter = new Zend_Config_Writer_Ini();

    // modify ini with according to input name and value; sanitize before writing

    // I haven't use an ini writer, but if the write() method returns a boolean,
    // store it in the success key
    $json['success'] = $iniWriter->write()

    $this->_helper->json($json);
}

jQuery部分:

$(document).ready(

    // say we have <p class="edit">edit</p><p>Value</p>

    // on click "edit", do DOM manipulation to turn into <p class="save">save</p><input name="inputname" value="some value" />

    $("p.save").on('click', function() {

        // somehow get the above input; I'm using it as a sibling; use "parents" or "children" etc.
        $input = $(this).siblings("input"); // $(this) is the current clicked element (<p>)

        // create data container to send to ZF
        var ajaxObj = {
            'inputName': $input.attr("name"),
            'inputVal': $input.val()
        };

        // manage the response as a JSON
        var callback = function(json) {
            if (json.success) {
                // change from input text to <p> to simulate edit-in-place
            } else {
                // alert? show error markup?
            }
        };

        // Remember "settingsEditAction" in the "AjaxController"? That's "settings-edit" in URL-speak as per ZF conventions
        // The last param is the data type you expect from server
        // Use POST for posting, GET for getting, what?
        $.post('/ajax/settings-edit', ajaxObj, callback, 'json');

    });

);

LE:
实际上,我错过了您点击修改的部分,p变成input
点击edit:第一个p已将“保存”作为文字,同级p成为输入。
点击save段:发送帖子;如果成功,请设置原始标记(2 <p>)。

答案 1 :(得分:0)

我想知道你是否真的需要ajax来解决这个问题。 Ajax会干净利落,但我认为如果没有它,你可以完成你想要的东西 我经常在一个页面上执行进程而无需导航。我发现如果我使用:

$this->_redirect($this->getRequest()->getRequestUri());

它只会刷新包含新信息的页面 事实上,似乎我总是在catch块中使用它来处理在flashMessenger中显示异常的异常:

catch (Zend_Exception $e) {

            $this->_helper->flashMessenger->addMessage($e->getMessage());
            $this->_redirect($this->getRequest()->getRequestUri());
        }

P.S。我会帮助Ajax,但我还没有做javascript ...(

相关问题