We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

How can i use jquery using the MVC?

This is my SignupController.php code:

<?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();
    }
}

Then this is my index.php inside my signup folder:

<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>

this is my 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');
});

what should I do?



37.0k

If after the submit is pressed it's redirected without hitting the js file I think you have to do it all using jquery, meaning on submit return false and do the form sending via ajax and on success show the suiccess message. Try to look around the forum for how to use ajax calls inside Phalcon.

Your problem here seems to be that you are mixing ajax calls with normal request / response.

If you want to stop the form from submitting, then you need to add

return false;

to the end of your jquery submit function.

You also need to perform the AJAX call to signup/registerwith your post data. Look here for more info on that:

Jquery AJAX Post

Oh, and I almost forgot. This line:

    $('#signup_feedback').html('Registration success', + name + 'has been registered. You may now do 10 cartwheels XD');

needs to go in your post success event function.

thank you! :D

On Thursday, July 3, 2014 8:39 PM, Gareth Williams [email protected] wrote:

Your problem here seems to be that you are mixing ajax calls with normal request / response.

If you want to stop the form from submitting, then you need to add

return false;

to the end of your jquery submit function.

You also need to perform the AJAX call to signup/registerwith your post data. Look here for more info on that:

Jquery AJAX Post — Reply to this email directly or view the complete thread on Phosphorum. Change your e-mail preferences here