R
R
raycheel2014-04-07 20:28:25
JavaScript
raycheel, 2014-04-07 20:28:25

How to add a category without refreshing the Yii page?

Hello! There was a need to quickly add a new category when adding a new article to the site. I realized that I need to use AJAX, created a form, made ajaxLink, wrote a handler for saving data.

Everything is fine, yes, the page reloads and I get to the form page, which is rendered through renderPartial.
Here is the handler:

public function ActionQuickcat()
  {
    $model = new CategoryForm;
    $form = new CForm($model->areasSettings(), $model);
    
    	if($form->submitted('submit') && $form->validate())
    	{
        	$newCat = new Coursecat;
        	$newCat->attributes=$_POST['CategoryForm'];

        	if($newCat->validate())
        	{
          	$newCat->save();
          	exit;
        	}
    	}
    $this->renderPartial('quickform', array('model'=>$model, 'form'=>$form));
  }


Here's the link:
echo CHtml::ajaxLink('Создать категорию', $this->createUrl('coursecat/quickcat'),
        array('update' => '.modal-body'),
        array('id'=>'login-btn', 'data-toggle'=>'modal', 'data-target'=>'#myModal','onclick' => "$('#myModal').modal('show')")
        );


How to make it so that it does not transfer to another page and the category appears in the list without reloading the page?

Thanks in advance!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
twixoff, 2014-04-10
@twixoff

The category creation form (the one in the modal window) needs to be sent by ajax for saving, and if successful:
1. close the modal window;
2. update the list of categories via ajax.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question