【问题标题】:How can i use jquery using the MVC?如何使用 MVC 使用 jquery?
【发布时间】:2014-07-03 02:41:52
【问题描述】:

这是我的 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();
}

有什么办法可以解决~~~非常感谢

【问题讨论】:

  • 我想你忘记了什么......$('#signupForm') 注册表格是一个 ID,所以不要忘记添加 '#'
  • 我已经更改了它,但无论我做什么,它总是重定向到注册。我想尝试不会重新加载我的页面的代码,但我输入的所有数据都将注册到我的数据库中。它只会显示它是使用 jquery 注册的注释。提前谢谢你:)
  • 这是一个更清晰的解释......在这种情况下,删除“
    ”标签并给你的按钮一个ID。当 some1 点击它触发你想在 javascript 中做的任何事情(例如进行 ajax 调用来进行注册)
  • 如果我删除了
    并把 id 放在我的按钮中,例如我把“boom”作为我的 id 在我的按钮中,我会用 $('#boom') 代替吗的 $('#signupForm') ?
  • 是的,类似的东西,如果你卡住了,去试试,我可以为你写一个简单的例子

标签: php jquery phalcon


【解决方案1】:

我不能 100% 确定您在这里实际问的是什么。但是基于 cmets,我是否正确假设您希望将用户存储在数据库中而不实际加载注册页面并仅显示一条消息?

如果是这样,我认为问题实际上似乎出在您的 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;

提交方法的底部

【讨论】:

  • 如果这是正确的答案,请您接受。谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-18
相关资源
最近更新 更多