Perfect - thanks. Here's a summary of my final changes.

After the submit button (using the Js helper):
echo $this->Js->writeBuffer(array('inline' => true));

This also means I don't have to give the submit button an id.

My login action in the users controller is:
function login() {
        Configure::write('debug', 0);
        $this->render('login', 'ajax');
}

And everything is working as expected.


Jeremy Burns
Class Outfit

jeremybu...@classoutfit.com
http://www.classoutfit.com

On 11 Jul 2010, at 20:27, nurvzy wrote:

> This was an issue I ran into while doing an ajax shopping cart.  The
> form element that is returned via AJAX needs to have echo $js-
>> writeBuffer() in it as well.  Alternatively, you can specify that the
> jshelper not buffer the javascript and instead output it as you call
> it (AjaxHelper style).
> 
> That solved my issue, and hope it helps you,
> NIck
> 
> On Jul 11, 4:22 am, Jeremy Burns <jeremybu...@classoutfit.com> wrote:
>> 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+unsubscr...@googlegroups.com For more options, visit this group at 
> http://groups.google.com/group/cake-php?hl=en

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+unsubscr...@googlegroups.com For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en

Reply via email to