普通网友 2012-12-10 21:05
浏览 48
已采纳

Zend Ajax提交表单

I have a little problem that I don't know how to solve. Ajax validation works perfect via zend. Pressing the submit button does validate, but when I got the right email and password it won't go to the user page. It just remains on that form. I know it has to do with the return false and true on the submit via Ajax.

Can someone help me to solve that please. I have no idea how to return true on the submit if it detect no errors on validation.

here's my code: Ajax and Javascript

$(function() {
    $('input').blur(function() {
        var formElementId = $(this).get(0).id;
        doValidation(formElementId);
    });
});

**$(function() {
    $('#btn_login').click(function() {
        var email = document.getElementById('email').id;
        var password = document.getElementById('password').id;
        doValidation(email);
        doValidation(password);
        return false;
    });
});**

function doValidation(id) {
    var url = '/users/validationform';
    var data = {};
    $('input').each(function() {
        data[$(this).attr('name')] = $(this).val();
    });
    $.post(url,data,function(resp) {
        $("#" + id).parent().find('.errors').remove();
        $("#" + id).parent().append(getErrorHtml(resp[id], id));
    }, 'json');
}

function getErrorHtml(formErrors, id) {
    var o = '<div id="login-error" style="color:red"><ul id="errors-'+id+'" class="errors" style="list-style-type:none;">';
    for(errorKey in formErrors) {
        o += '<li>- ' + formErrors[errorKey] + '</li>';
    }
    o += '</ul></div>';
    return o;
}

Zend controller:

public function validationformAction()
{
    $loginForm = new Application_Form_UserLogin();
    $loginForm->isValidPartial($_POST);
    header('Content-type: application/json');
    $this->_helper->json($loginForm->getMessages());
}
public function loginFormAction()
{
    $this->_helper->layout()->disableLayout();
    //$this->_helper->viewRenderer->setNoRender(true);

    $email = $this->getRequest()->getParam('email');
    $password = $this->getRequest()->getParam('password');

    $loginForm = new Application_Form_UserLogin();
    if ($this->getRequest()->isPost())
    {
        /************ Login Form ************/
        if ($loginForm->isValid($this->getRequest()->getParams()))
        {
            $user = $this->_helper->model('Users')->createRow($loginForm->getValues()); 
            $user = $this->_helper->model('Users')->fetchRowByFields(array('email' => $email, 'hash' => $password));

            if($user) 
            {
                Zend_Session::rememberMe(86400 * 14);
                Zend_Auth::getInstance()->getStorage()->write($user);
                $this->getHelper('redirector')->gotoRoute(array(), 'invite');
                return;
            } 
            else {
                // Error message
                $this->view->errorMsg = "<b>password</b> - invalid, please try again! *";
            }               
        }
        else
        {
            // something
        }
    }       
    $this->view->loginForm = $loginForm;
}
  • 写回答

1条回答 默认 最新

  • duanjihe5180 2012-12-11 13:14
    关注

    Try the submit method, like this: $(formselector).submit(); But because you used ajax validation, the real problem here that when you submit. I think you can write the separated validation function for the all field, and you can use the ajax callback to submit.

    Or try the jquery.validation http://bassistance.de/jquery-plugins/jquery-plugin-validation/ because it know the ajax server side validation, and auto submit the form when is valid.

    Sry my english, i from hungary :D

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号