2012-02-11 59 views
1

目前我创造我自己的CMS和我有一个很难回答的问题(我认为这是)我不能发现在谷歌什么...与Zend_Config_Ini的AJAX和PHP

我试图让一个edit_settings.php与AJAX和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,但我不知道如何将它们连接在一起......

的问题:我怎么能这样做呢?

+1

你见过这个吗? http://www.appelsiini.net/projects/jeditable – vascowhite 2012-02-11 10:11:43

回答

1

我不会去整个页面刷新的事情。它增加了额外的逻辑控制器。逻辑可能很容易在专业阿贾克斯控制器和jQuery的AJAX是冷静和AJAX是完美的编辑就地东西

我会做如下假设:。

  • 你使用jQuery

  • 你知道有效的JSON字符串成为j中的一个JS对象查询

  • 你有一个AjaxController以“settingsEditAction”

  • 你知道,这是我的采取你的问题,而不是强加的 思考任何风格,甚至编码的;这是如何会做到这一点,所以这是坏 还是不错的。

  • 你知道我在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>'s)。

0

我不知道你是否真的需要ajax这个问题。阿贾克斯将是整洁干净的,但我认为你可以在没有它的情况下完成你想要的任务。
我经常在单个页面上执行进程而无需导航。我发现如果我使用:

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

它只是刷新页面的新信息。
事实上它似乎我总是用这个在catch块例外以FlashMessenger显示例外:

catch (Zend_Exception $e) { 

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

附:我会帮助Ajax,但我不会做JavaScript ...但:(我不会去做javascript ...但:(