Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
381 views
in Technique[技术] by (71.8m points)

2 AJAX calls in the same view (CakePHP 2.3)

I have problem with AJAX in CakePHP. I have 2 different AJAX forms in one view. The first AJAX form works well, but the other one doesn't work properly. When I call the second AJAX, it doesn't set data from this form to $this->request->data, but the AJAX will run properly. After that, it will update <div id="about"></div>. Curious is fact, that after the div is updated and I try send data through this form again, it works and it will update my data in database. Also when I have only the second AJAX in the view, it will work properly and send the data first time I send the data through it.

Here's my code:

This is a view file:

<div id="price-list">
    <?php echo $user['User']['price_list']; ?>
</div>

<?php
    echo $this->Form->create('User');
    echo $this->Form->input('User.price_list', array('label' => false));
    echo $this->Js->submit('Save', array(
        'url' => array('controller' => 'users', 'action' => 'ajax_edit_price_list'),
        'update' => '#price-list',
        'buffer' => false,
    ));
?>

<div id="about">
    <?php echo $user['User']['about']; ?>
</div>

<?php
    echo $this->Form->create('User');
    echo $this->Form->input('User.about', array('label' => false));
    echo $this->Js->submit('Save', array(
        'url' => array('controller' => 'users', 'action' => 'ajax_edit_about'),
        'update' => '#about',
        'buffer' => false,
    ));
?>

I have also append <?php echo $this->Js->writeBuffer(); ?> in my default.ctp before </body>.

Do you have any idea, where can be a problem? Thanks

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I wasn't able to reproduce your exact issue, but I think I know the problem. Your second form is being created inside of the first form. You need to add echo $this->Form->end(); to the end of both forms.

Also, the forms are getting created with the same ID. Although this isn't what's causing the problem, it's still not good. I suggest you take thaJeztah's advice and specify an ID manually inside $this->Form->create().


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...