Js helper advice needed
I have a view that contains an element for logging in. The key bits
are inside a div with an id of 'login'. The login form's submit button
is created using the Js helper:
echo $this->Js->submit(
'Login',
array(
'update' => '#login',
'url' => array(
'controller' => 'users',
'action' => 'login'
),
'id' => 'sbt_login'
)
);
I have given the button an id so that any Javascript created will
reference that, rather than a random number.
When the view is rendered, this is the HTML for the form:
<div id="login">
<h3>Login/Register</h3>
<form controller="users" id="UserLoginForm" method="post" action="/
users/login" accept-charset="utf-8">
<div style="display:none;">
<input type="hidden" name="_method" value="POST" />
</div>
<label for="UserUsername">Username</label>
<input name="data[User][username]" type="text" id="UserUsername" />
<label for="UserPassword">Password</label>
<input type="password" name="data[User][password]"
id="UserPassword" />
<div class="submit">
<input id="sbt_login" type="submit" value="Login" />
</div>
</form>
</div>
Using $this->Js->writeBuffer, the following script is added to the
body:
<script type="text/javascript">
//<![CDATA[
$("#sbt_login").bind("click", function (event) {$.ajax({data:$
("#sbt_login").closest("form").serialize(), dataType:"html",
success:function (data, textStatus) {$("#login").html(data);},
type:"post", url:"\/users\/login"});
return false;});});
//]]>
</script>
So far so good. Clicking the 'Log in' button triggers the login
methods of the users controller using ajax and refreshes the contents
of the 'login' div.
Assuming that the login failed, the login form is redrawn, along with
an error message. The HTML is exactly the same. This time however,
clicking the login button does not use ajax, so the whole page is
replaced with just the contents of the login div.
One way around this is to not include the login submit button in the
HTML that is refreshed via ajax. The problem with this is that if the
login is successful, the login button should disappear, which of
course it won't.
I could do all of this with straightforward javascript, but that sort
of misses the point of having the Js helper.
What am I doing wrong, what is the right way to do this and are there
any examples out there?
Check out the new CakePHP Questions site http://cakeqs.org and help others with their CakePHP related questions.
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to
cake-php+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php?hl=en
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home