On a given form input field, on blur, I would like to check if that input value is valid.
To know if that id is valid or not, I need to query the database. If it's not valid, a message should appear next to the input field indicating that the option is not valid.I'm over Zend Framew开发者_开发知识库ork, however, this is my very first solo Ajax experience.
Can I have a skeleton about how something like this could be implemented please ?
I'm a little aware that something like this should be used...
if($this->getRequest()->isXmlHttpRequest()) {
...
but I really need a help here. :s
Thanks a lot,
MEMFirst thing you should take care while handling AJAX requests in Zend Framework - disable the view/MVC layout component.
In your action,
public function validateAction()
{
if($this->getRequest()->isXmlHttpRequest()) {
//Disable the view/layout
$this->_helper->layout->disableLayout();
$this->_helper->viewRenderer->setNoRender(TRUE);
//Receive the value from the form
$inputValue = $this->_getParam('name');
//Access your model and validate the data.
$model = new Model();
$result = $model->isValid($inputValue);
$myArray = array(
'result'=>$result
);
$jsonData = Zend_Json::encode($myArray);
//Send the result back to the client
$this->response->appendBody($jsonData);
}
}
Receive this JSON object from client side(use jQuery), process it and show the appropriate message.
A similar question - How do you make Zend Framework NOT render a view/layout when sending an AJAX response?
精彩评论