How To Create An Ajax Form That Create A Record.

Helly everyone.

I want to create an Ajax button that will create a record, but I don’t have the Ajax concept, would anyone please create it for me.

The button should create a record and on successful creation it resides on the same page and just show a popup window to show that the "Item is added to the wishlist sucessfuly".

I have a wiki but due to lack of ajax knowledge I could do this for my requirement.

the wiki link is http://www.yiiframework.com/wiki/388/ajax-form-submiting-in-yii/

the button code is


<?php echo CHtml::link(Yii::t('site', 'Add to Wishlist'), array('/wishlist/create', 'item'=>$model->id, 'provider'=>$model->seller_id), array('class'=>'btn btn-lg btn-success')) ?>

and the wishlist form (which will create a wishlist record of the current user) is


<?php $form=$this->beginWidget('bootstrap.widgets.TbActiveForm',array(

	'id'=>'wishlist-form',

	'enableAjaxValidation'=>false,

)); ?>


	<p class="help-block">Fields with <span class="required">*</span> are required.</p>


	<?php echo $form->errorSummary($model); ?>


	<?php echo $form->hiddenField($model,'item_id',array('class'=>'form-control', 'value'=>$_GET['item'])); ?>


	<?php echo $form->hiddenField($model,'user_id',array('class'=>'form-control,  'value'=>$_GET['provider']')); ?>




	<div class="form-actions">

		<?php $this->widget('bootstrap.widgets.TbButton', array(

			'buttonType'=>'submit',

			'type'=>'primary',

			'label'=>$model->isNewRecord ? 'Create' : 'Save',

		)); ?>

	</div>


<?php $this->endWidget(); ?>



Thanks in advance.

The button you’re adding is not an ajax button. use CHtml::ajaxButton instead. Then send the JSON data to a controller, decode it with json_decode, then save the data in you database as you would do in an actionCreate.

If you want to return anything use echo instead of return. Ajax will get the echo and you will be able to to show it when the action is completed.

Here you got a few examples: http://stackoverflow.com/questions/13837333/ajax-button-post-in-yii

dear by ajax button I mean a form that will create a record using ajax concept, I have updated my post, please have a look on it.