I'm currently creating my own CMS and I have a hard question (I think it is) and I can't found anything on Google...
I'm trying to make a edit_settings.php
with AJAX and PHP, to include my Zend_Config_Ini
into it 'cause I'm using this for my config.ini
parsing and writing.
There's is the base of the PHP code (my array isn't there, normal):
<?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();
?>
Exemple: I click on "Edit" and the red-bordered zone become a input field and I can edit it without changing page and I just need to click "Save" after to save the config in the config.ini
file.
I know that this needs AJAX, PHP and the Zend_Config_Ini
but I don't know how to link them together...
The question: HOW I CAN DO THIS?
I wouldn't go with the whole page refresh thing. It adds additional logic to the controller. Logic which could be easily be in specialized ajax controllers. And jQuery's AJAX is cool. And AJAX is perfect for edit-in-place stuff.
I'll make the following assumptions:
you use jQuery
you know that a valid JSON string becomes a JS object in jQuery
you have an AjaxController with a "settingsEditAction"
you know that this is my take on your problem, not imposing any style of thinking or even of coding; this is how I would do it, so it's bad or good.
you know I've written this in np++, off the top of my head, so you won't freak out on missing colons or the like
In 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);
}
The jQuery part:
$(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:
Actually, I've missed the part in which you click edit, and the p
turns into an input
.
Click edit
: 1st p
has "save" as text, and the sibling p
becomes an input.
Click save
paragraph: send post; if success, set the original markup (2 <p>
's).
I wonder if you really need ajax for this issue. Ajax would be neat and clean but I think you can accomplish what you want without it.
I often execute processes on a single page without ever navigating away. I find that if I use:
$this->_redirect($this->getRequest()->getRequestUri());
It will just refresh the page with the new information.
In fact it seems I always use this in a catch block for exceptions to display exceptions in the flashMessenger:
catch (Zend_Exception $e) {
$this->_helper->flashMessenger->addMessage($e->getMessage());
$this->_redirect($this->getRequest()->getRequestUri());
}
PS I would help with the Ajax, but I don't do javascript... yet :(
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.