Yii Framework Forum: Ajax Submit button - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Ajax Submit button plz help a new Rate Topic: -----

#1 User is offline   tydeas_dr 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 158
  • Joined: 16-April 10
  • Location:Greece

Posted 06 May 2010 - 08:56 AM

Hello ppl,
I will try to be give as may information as possible.
I have a model person where in _form.php view i have this code:

<div id="doy">
<?php echo $form->dropDownList($person,'doyid',CHtml::listData(Doy::model()->findAll(),'doyid','doyname'),array('prompt'=>'Select')); ?>
<?php echo CHtml::ajaxLink("Add new Doy.",$this->createUrl('/doy/addnew'),array('replace'=>'#doy'));?>
</div>


So there is a dropdownlist feeded with data from an other model Doy and a ajaxLink in case you can not find what you want and be able to add it with the function of the models (doy) addnew action which is the following:

	public function actionAddnew()
	{
		$model=new Doy;
                $this->performAjaxValidation($model); // I want to perform validation
                if(isset($_POST['Doy'])){
                    $model->attributes=$_POST['Doy'];
                    if($model->save()){
                        echo "test";
                    }
                } else {
                    $this->renderPartial('_form_1',array('doy'=>$model),false,true);
                }
        }


For the moment keep in mind the `echo "test";` line.
The very first time when the actionAddnew will be called the _form_1 will be rendered.Here is _form_1.php view:

<div class="wide form" >
<?php $form=$this->beginWidget('CActiveForm', array(
	'id'=>'doy-form',
	'enableAjaxValidation'=>true,
)); ?>
	<?php echo CHtml::errorSummary($doy); ?>

	<div class="row">
		<?php echo $form->labelEx($doy,'doyid'); ?>
		<?php echo $form->textField($doy,'doyid',array('size'=>20,'maxlength'=>20)); ?>
		<?php echo $form->error($doy,'doyid'); ?>
	</div>

	<div class="row">
		<?php echo $form->labelEx($doy,'doyname'); ?>
		<?php echo $form->textField($doy,'doyname',array('size'=>60,'maxlength'=>90)); ?>
		<?php echo $form->error($doy,'doyname'); ?>
	</div>
	<div class="row buttons">
		<?php echo CHtml::ajaxSubmitButton(Yii::t('forms','Create'),CHtml::normalizeUrl(array('doy/addnew'),array('replace'=>'#doy'))); ?>

	</div>
<?php $this->endWidget(); ?>
</div><!-- form -->


Ok...where is the problem. When i visit the _pform there is the dropdown list (codesnippet1) when i clik on the ajaxLink there the form renders proper replacing the dropdownlist. I insert values it validate them and i press the create of the _form_1 which is the ajaxSubmitButton. The values are submiting but the form is not replaced as it should from the echo "test";
Of course i dont want only test to be rendered but in fact i want a new dropdownlist with the values submitted previously to be the selected. Please help because i am new to ajax and yii.

What is the problem? If you want more information please inform.
Follow me on twitter @dmtrsslvdr
Check my *new* blog @ tumblr
0

#2 User is offline   tri 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 1,651
  • Joined: 20-November 08
  • Location:Stockholm, Sweden

Posted 06 May 2010 - 10:13 AM

For a start, try 'update' instead of 'replace'. (update will replace the div tag with the renderPartial content).

/Tommy
Don't forget to read The Definitive Guide to Yii (en) (sv) | The class reference has the details
0

#3 User is offline   tydeas_dr 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 158
  • Joined: 16-April 10
  • Location:Greece

Posted 06 May 2010 - 11:39 AM

Ok i changed in both ajax the replace to update. It didn't happened what i was expecting. I used firephp so i can debug it and show you. I changed the actionAddnew like this....
	public function actionAddnew()
	{
		$model=new Doy;
                $this->performAjaxValidation($model);
                if(isset($_POST['Doy'])){
                    
                    $model->attributes=$_POST['Doy'];
                    if($model->save()){
                        echo "test";

                    }
                } else {
                    fb("edw giati???"); //debuging
                    $this->renderPartial('_form_1',array('doy'=>$model),false,true);
                }
        }


  • Load page
    Posted Image
  • Then click the ajaxLink and renders.
    Posted Image
  • Then i enter some values to the textfield and validation occurs.
    Posted Image
  • And finally i press create.
    Posted Image


At final step i get the $this->renderPartial('_form_1',.....); althought it's in else case, strange?!?! and of course test.
What i noticed is that the renderPartial comes with GET and the test with POST i will see if this is a problem now.But how can i changes this? :huh:

Any ideas welcome.

Sorry about the size of the pics but i didn't know how to resize it smaller here in forum.
Something i noticed now is that both ajaxLink and ajaxSubmitButton come with id 'yt0' is this maybe the problem?
Follow me on twitter @dmtrsslvdr
Check my *new* blog @ tumblr
0

#4 User is offline   tri 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 1,651
  • Joined: 20-November 08
  • Location:Stockholm, Sweden

Posted 06 May 2010 - 01:03 PM

Quote

Something i noticed now is that both ajaxLink and ajaxSubmitButton come with id 'yt0' is this maybe the problem?


You can try setting a unique name attribute, htmlOptions is the fourth parameter to ajaxLink/ajaxsubmitButton
array('id'=>'unique_name')

Edit: should be 'id', not 'name'

/Tommy

This post has been edited by tri: 06 May 2010 - 04:18 PM

Don't forget to read The Definitive Guide to Yii (en) (sv) | The class reference has the details
1

#5 User is offline   tydeas_dr 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 158
  • Joined: 16-April 10
  • Location:Greece

Posted 06 May 2010 - 04:45 PM

I left the ajaxLink (1st) to update and the ajaxSubmitButonn (2nd) at replace like.
_form
<?php echo CHtml::ajaxLink("Add new Doy.",$this->createUrl('/doy/addnew'),array('update'=>'#doy'));?>

_form_1
<?php echo CHtml::ajaxSubmitButton(Yii::t('forms','Create'),CHtml::normalizeUrl(array('doy/addnew')),array('replace'=>'#doy','type'=>'POST'),array(
                //test
                )); ?>

and worked like it should echoing test.
No other issues related to how the "... i dont want only test to be rendered but in fact i want a new dropdownlist with the values submitted previously to be the selected. Please help because i am new to ajax and yii." as described in topic
Follow me on twitter @dmtrsslvdr
Check my *new* blog @ tumblr
0

#6 User is offline   tri 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 1,651
  • Joined: 20-November 08
  • Location:Stockholm, Sweden

Posted 06 May 2010 - 05:37 PM

View Posttydeas_dr, on 06 May 2010 - 04:45 PM, said:

I left the ajaxLink (1st) to update and the ajaxSubmitButonn (2nd) at replace like.
_form
<?php echo CHtml::ajaxLink("Add new Doy.",$this->createUrl('/doy/addnew'),array('update'=>'#doy'));?>

_form_1
<?php echo CHtml::ajaxSubmitButton(Yii::t('forms','Create'),CHtml::normalizeUrl(array('doy/addnew')),array('replace'=>'#doy','type'=>'POST'),array(
                //test
                )); ?>

and worked like it should echoing test.
No other issues related to how the "... i dont want only test to be rendered but in fact i want a new dropdownlist with the values submitted previously to be the selected. Please help because i am new to ajax and yii." as described in topic


I suggest you read this cookbook article
http://www.yiiframew...oc/cookbook/24/
It describes how to update a dropdown already present in the page.

You also may want to read this thread.

/Tommy
Don't forget to read The Definitive Guide to Yii (en) (sv) | The class reference has the details
0

#7 User is offline   tydeas_dr 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 158
  • Joined: 16-April 10
  • Location:Greece

Posted 06 May 2010 - 06:51 PM

Yes i have seen this before (cookbook dependent dropdownlist) The problem is that the dropdownlist does not exist any more because the <div> containing the list has been update it with 1st ajaxLink.
The solution i am thing is that the dropdownlist will be hidden and after the ajaxSubmit call (2nd) will be feed with data and appeared again.

Any other suggestions or corrections welcome.
Follow me on twitter @dmtrsslvdr
Check my *new* blog @ tumblr
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users