Bizarre Ajax Issue

Fixed - Check last post

Ok so here’s a rough explanation of what I’m doing. Building a registration form that has a privacy agreement before showing the form. It’s loaded first and then the form replaces the agreement (when the user accepts) through an Ajax request.

It works and is simple (a little code messy but simple - still playing with it) but I get one tiny little issue lol…

Take a look at the screen shot: 712

Duplicate Application.PNG

So for some reason the web app gets duplicated and replaces in the div through the ajax request.

Here’s the code, the registerPrivacy.php file is just html so that’s not affecting it. Without the ‘update’=>’…’ ajax won’t work and thus doesn’t pose the problem but then obviously my idea doesn’t work.

Maybe there’s a simpler way of doing this?




<?php

$this->breadcrumbs=array(

    'Users'=>array('index'),

    'Register',

);


$this->menu=array(

    array('label'=>'List Users', 'url'=>array('index')),

    array('label'=>'Manage Users', 'url'=>array('admin')),

);

?>


<?php if(Yii::app()->user->hasFlash('register')): ?>


<div class="flash-success">

    <?php echo Yii::app()->user->getFlash('register'); ?>

</div>


<?php else: ?>


<div id="RegisterAgreement">

<?php 

if((isset($_GET['continue'])) && ($_GET['continue']=='yes'))

{

?>

<h1>Register</h1>

<?php

echo $this->renderPartial('_form', array('model'=>$model), true, false);              

}

else

{

    include_once 'protected/modules/users/views/users/registerPrivacy.php';

    

    echo CHtml::ajaxButton('I understand & agree', array('', 'continue'=>'yes'),array('type'=>'GET','update'=>'#RegisterAgreement')); 

}

?>

</div><!-- Close RegisterAgreement -->

<?php endif; ?>



Any help would be appreciated :)

You need to show us the controller code.

Remember - render() renders the view WITH the layout, renderPartial() renders it without the layout. On the ajax request, you should renderPartial() instead of render(), and you should only render what you want to get placed in #RegisterAgreement

I see well it’s the basic setup from CRUD statements just changed from create to register:




/**

     * Guests Registers a new model.

     * If creation is successful, the browser will be redirected to the 'view' page.

     */

    public function actionRegister()

    {

        $model=new Users;

        

        // Set the scenario for validation

        $model->setScenario('register');


        // Uncomment the following line if AJAX validation is needed

        $this->performAjaxValidation($model);


        if(isset($_POST['Users']))

        {

            $model->attributes=$_POST['Users'];

            if($model->save())

            {

                Yii::app()->user->setFlash('register','Thank you for Registering with us. An admin will activate your account as soon as possible. Contact webmaster@sterlingsavvy.com for any issues.');

                $this->redirect(array('view','id'=>$model->id));

            }

        }


        $this->render('register',array(

            'model'=>$model,

        ));

    }



What are you suggestions?

As jonah said

1- testing if it is an Ajax request

I would make a test in case where ajax is not possible, to render something valid

2- if it is the case, replace render by renderPartial.

With ajax you launch small pieces of data and you do not have to render ALL the view and layout only the view where the new data are displayed

Replacing render with renderPartial in the register action doesn’t show anything but the view file (so no styling, no menus nothing). The initial call needs to be made as a render but from that point on I realize I have to use a render partial, so should I be putting in the logic that’s in the view through to the controller instead?

Fixed it! Thanks guys, the advice enlightened me a bit ;)

New register action code:




    /**

     * Guests Registers a new model.

     * If creation is successful, the browser will be redirected to the 'view' page.

     */

    public function actionRegister()

    {

        $model=new Users;

        

        // Set the scenario for validation

        $model->setScenario('register');


        // Uncomment the following line if AJAX validation is needed

        $this->performAjaxValidation($model);

        

        if(isset($_POST['Users']))

        {

            $model->attributes=$_POST['Users'];

            if($model->save())

            {

                Yii::app()->user->setFlash('register','Thank you for Registering with us. An admin will activate your account as soon as possible. Contact webmaster@sterlingsavvy.com for any issues.');

                $this->redirect(array('view','id'=>$model->id));

            }

        }


        if((isset($_GET['continue'])) && ($_GET['continue']=='yes'))

        {

            $display = $this->renderPartial('_form', array('model'=>$model), true, false);

            $this->renderPartial('register',array(

                'model'=>$model,

                'display'=>$display,

            ));

        }

        else

        {

            $display = $this->renderPartial('registerPrivacy', array('model'=>$model), true, false);

            $this->render('register',array(

                'model'=>$model,

                'display'=>$display,

            ));             

        }

    }



View codes:

For Register




<?php

$this->breadcrumbs=array(

    'Users'=>array('index'),

    'Register',

);


$this->menu=array(

    array('label'=>'List Users', 'url'=>array('index')),

    array('label'=>'Manage Users', 'url'=>array('admin')),

);

?>


<?php if(Yii::app()->user->hasFlash('register')): ?>


<div class="flash-success">

    <?php echo Yii::app()->user->getFlash('register'); ?>

</div>


<?php else: ?>


<div id="RegisterAgreement">

<?php 

echo $display;

?>

</div><!-- Close RegisterAgreement -->

<?php endif; ?>



For Register Privacy




<h1>Registration Privacy Agreement</h1>


<p>All the privacy information will go here</p>


<br/>

<?php 

echo CHtml::ajaxButton('I understand & agree', array('', 'continue'=>'yes'),array('type'=>'GET','update'=>'#RegisterAgreement'));

?>