【问题标题】:using ajax in zend framework 1.12在 zend 框架 1.12 中使用 ajax
【发布时间】:2013-09-29 08:50:19
【问题描述】:

我是 zend 框架和 ajax 的初学者 我尝试用 ajax 和 zend 做一个简单的代码, 它包含一个 div , text 和 button 当按下按钮时,文本的值被填充到 div 但是,当我按下按钮时,什么也没发生 :S 这是我的代码,请帮助我

这是布局

/index/test.phtml


<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"       type="text/javascript"></script>

<!--for display text and submit fields i use viewHelpers it's very comfortable  and       easy way.-->
<div class="submit_area">
  <?php echo $this->formText('message', 'empty', array('size' => 32, 'id' => 'message')) ?>
  <?php echo $this->formSubmit('submitTo', 'submit', array('id' => 'submitTo')) ?>
  <div class="show-msg"></div>
</div>

<script >
//for send data i'll use jquery library
$(document).ready( function() {
  <?php echo "ready" ?>
  //By clicking on button submit condition makes validate on empty message 
  //unless field message will be not empty  , the programm make send data to
  //controller via ajax
  $("#submitTo").click(function() {
    var message = $('#message').val();
    if (message != '') {
      //run ajax 
      $.post('index/ajax', {'message' : message},
        //callback function
        function (respond) {
          //put respond in class show-msg                          
          $(".show-msg").html(respond);                  
        }
      );
    }                    
  });
});
</script>

--------------------------------------------

这是控制器中的代码


class IndexController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
    }

    public function checkAction()
    {
        // action body  
        $request = $this->getRequest()->getPost();

        //referring to the index
        //gets value from ajax request
        $message = $request['message'];

        // makes disable renderer
        $this->_helper->viewRenderer->setNoRender();

        //makes disable layout
        $this->_helper->getHelper('layout')->disableLayout();

        //return callback message to the function javascript
        echo $message; 
    }

    public function testAction()
    {
        // action body      
    }
}

【问题讨论】:

    标签: php ajax zend-framework


    【解决方案1】:

    你有什么错误吗?

    我可以在您的代码中看到几处可能是错误的。

    1. click() 事件不是 return false,因此您的表单可能在 Ajax 调用发生之前提交。

    2. Ajax URL 指向/index/ajax,这意味着IndexController 应该有一个ajaxAction。您应该将post() 请求指向/index/check

    您可能还想看看ContextSwitch and AjaxContext

    确实@Boulevard 说的也是正确的,echo 破坏了你的 JS。

    【讨论】:

    【解决方案2】:

    您在 javascript 语法上的代码错误。 尝试删除此行:

      <?php echo "ready" ?>
    

    并添加javascript关闭标签:

    </script>
    

    【讨论】:

    • thnxx 我删除了它,但 div 中仍然没有出现值
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多