我如何使用MVC使用jquery?

时间:2014-07-03 02:41:52

标签: php jquery phalcon

这是我的SignupController.php代码:

    <?php class SignupController extends \Phalcon\Mvc\Controller {

public function indexAction()
{

}

public function registerAction()
{

$user = new Users();

    //Store and check for errors
    $success = $user->save($this->request->getPost(), array('name', 'email'));

    if ($success) {
        echo "k";

    } else {
        echo "Sorry, the following problems were generated: ";
        foreach ($user->getMessages() as $message) {
            echo $message->getMessage(), "<br/>";
        }
    }

    $this->view->disable();
}  }

然后这是我的注册文件夹中的index.php:

   <html>
   <head><title>trial</title> <script type="text/javascript" src="public/js/jquery.js"></script>
   <script type="text/javascript" src="public/js/ext.js"></script>
   <body>
   <div id="signup/-feedback"></div>
   <form id="signupForm" name="loginForm" method="post" action="signup/register">
   <table width="300" border="0" align="center" cellpadding="2" cellspacing="0"  >
   <td colspan="3" align="center" height="50"><strong>Registration form</strong></td>  <br />
   <tr>
   <th>Name:</th>
   <td><input name="name" type="Variable" class="input2" id="name"  /></td>
   <div id="underInput" />
   </tr>
   <tr>
   <th>Email:</th>
   <td><input name="email" type="Variable" class="input2" id="email" /></td>
   </tr>

  <td>&nbsp;</td>

  <td><input type="submit" name="Submit" value="Register" />
  <input type="reset" name="Submit2" value="Clear" /></td>
  </tr>
  </table>

  </body>
  </html>

这是我的ext.js:

$('signupForm').submit(function(){
    var name = $ ('#name').val();
    $('#signup_feedback').html('Registration success', + name + 'has been registered. You may now do 10 cartwheels XD');
});

我该怎么办?

这是我公共文件夹中的index.php:         

    try {

//Register an autoloader
$loader = new \Phalcon\Loader();
$loader->registerDirs(array(
    '../app/controllers/',
    '../app/models/'
))->register();

//Create a DI
$di = new Phalcon\DI\FactoryDefault();

//Setup the database service
$di->set('db', function(){
    return new \Phalcon\Db\Adapter\Pdo\Mysql(array(
        "host" => "localhost",
        "username" => "root",
        "password" => "123",
        "dbname" => "test_db"
    ));
});

//Setup the view component
$di->set('view', function(){
    $view = new \Phalcon\Mvc\View();
    $view->setViewsDir('../app/views/');
    return $view;
});

//Setup a base URI so that all generated URIs include the "tutorial" folder
$di->set('url', function(){
    $url = new \Phalcon\Mvc\Url();
    $url->setBaseUri('/');
    return $url;
});

//Handle the request
$application = new \Phalcon\Mvc\Application($di);

echo $application->handle()->getContent();

} catch(\Phalcon\Exception $e) {
 echo "PhalconException: ", $e->getMessage();
}

有没有办法让我解决这个问题~~~非常感谢你

1 个答案:

答案 0 :(得分:0)

我不能100%确定你在这里真正要求的是什么。但根据评论我是否正确,假设您想要将用户存储在数据库中而不实际加载注册页面并只显示消息?

如果是这样,我认为问题实际上似乎与您的javascript:

   $('#signupForm').submit(function(){
   var name = $ ('#name').val();
    $('#signup_feedback').html('Registration success', + name + 'has been registered. You may now do 10 cartwheels XD');
 });

你在这里所做的一切都是为name变量赋值,然后更改id为signup_feedback的元素的html。您实际上并没有停止表单的操作。您可以通过向此方法添加警报来测试此功能,您将看到警报火,然后重定向。你需要在这里使用AJAX将name变量发布到寄存器表单,然后阻止表单的默认操作,从而停止重定向

类似下面的伪代码

     $('#signupForm').submit(function(e){ //note the e variable this is the event
     e.preventDefault();
     var name = $('#name').val(); // get the value of name from the form
        $.ajax({
          type: "POST",
           url: "signup/register",
           data: { name: name }
        })
       .done(function( msg ) {
             $('#signup_feedback').html('Registration success', + name + 'has been registered. You may now do 10 cartwheels XD');
        });
     });

AJAX函数的完整文档在这里http://api.jquery.com/jQuery.ajax/

正如我所说,这只是伪代码所以它可能需要一些调整,但它应该指向正确的方向。 e.preventDefault()的替代方法就是添加

   return false;

到提交方法的底部