Jquery/Ajax post

Hi

I am trying to post a variable from a view/textfield to controller. I am unable to do so. Below is the code for controller and view. I have highlighted what is not working.

View code

<div class="form">

<?php echo CHtml::beginForm()?>

<?php echo CHtml::activeLabel($model2, "username")?>

<?php echo CHtml::activeLabel($model2, "email")?>

[b]<?php echo CHtml::activeTextField($model2, “email”,array(‘id’=>‘foo’))?>

[/b]

<ul class="tasks">

	&lt;?php /*?&gt;&lt;?php &#036;this-&gt;renderPartial('_task', array('model' =&gt; &#036;model2));?&gt;&lt;?php */?&gt;

</ul>

 &lt;div class=&quot;row buttons&quot;&gt;


&lt;?php echo CHtml::button('Add Player',array('class' =&gt; 'tasks-add'))?&gt;


  &lt;?php Yii::app()-&gt;clientScript-&gt;registerCoreScript


  (&quot;jquery&quot;)?&gt;


  &#60;script&gt;


  &#036;(&quot;.tasks-add&quot;).click(function(){


  &#036;.ajax({


  success: function(html){


  &#036;(&quot;.tasks&quot;).append(html);


  },


  type: 'get',


  url: '&lt;?php echo &#036;this-&gt;createUrl('field')?&gt;',


 [b] data: {


  index:&#036;(&quot;#foo&quot;).val()[/b]


  },


  cache: false,


  dataType: 'html'


  });

});

</script>

</div>

<?php echo CHtml::endForm()?>


Controller code

class HomeController extends Controller

{

public function filters()


{


return array(


'ajaxOnly + field'


);


}

public function actionIndex()

{

	&#036;model= new Teamname;


	&#036;model1= new Gametype;


	&#036;select = array(); 


	&#036;model1 = Gametype::model()-&gt;findAll();


            &#036;model2 = new User();


        &#036;this-&gt;render('createteam', array('model'=&gt;&#036;model,'model1'=&gt;&#036;model1,'model'=&gt;&#036;model,  'model2'=&gt;&#036;model2,  'select'=&gt;&#036;select));








}

[b]public function actionField($index)

{


	


	if(isset(&#036;_POST['index']))


{


			


	&#036;model2 = new User();


			 &#036;this-&gt;renderPartial('_task', array('model' =&gt; &#036;model2));


		


		}


	}

}

[/b]

As far as i can see, you are doing a GET request and in controller you check $_POST, which of course is empty.

Hi,

Thanks, I switched get to post.

I think I am getting the post but the issue is different.

In firebug, I am getting

RESPONSE

[b]<h1>CHttpException</h1>

<p>Your request is invalid. (C:\xampp\htdocs\yii\framework\web\CController.php:328)</p><pre>#0 C:\xampp\htdocs\yii\framework\web\CController.php(301): CController->invalidActionParams(Object(CInlineAction))

#1 C:\xampp\htdocs\yii\framework\web\filters\CFilterChain.php(134): CController->runAction(Object(CInlineAction))

#2 C:\xampp\htdocs\yii\framework\web\CController.php(1129): CFilterChain->run()

#3 C:\xampp\htdocs\yii\framework\web\filters\CInlineFilter.php(59): CController->filterAjaxOnly(Object(CFilterChain))

#4 C:\xampp\htdocs\yii\framework\web\filters\CFilterChain.php(131): CInlineFilter->filter(Object(CFilterChain))

#5 C:\xampp\htdocs\yii\framework\web\CController.php(283): CFilterChain->run()

#6 C:\xampp\htdocs\yii\framework\web\CController.php(257): CController->runActionWithFilters(Object(CInlineAction), Array)

#7 C:\xampp\htdocs\yii\framework\web\CWebApplication.php(277): CController->run(‘field’)

#8 C:\xampp\htdocs\yii\framework\web\CWebApplication.php(136): CWebApplication->runController(‘home/field’)

#9 C:\xampp\htdocs\yii\framework\base\CApplication.php(158): CWebApplication->processRequest()

#10 C:\xampp\htdocs\mylls1\index.php(13): CApplication->run()

#11 {main}</pre>[/b]


HTML

[b]CHttpException

Your request is invalid. (C:\xampp\htdocs\yii\framework\web\CController.php:328)[/b]


Post

index vikas02121@yahoo.com

Source

index=vikas02121%40yahoo.com

Regards,

Vic

Look into your console (FireBug/Developer tools etc) and see the xhr request, watch the url and the params and see if it is what you want.

I did not follow what you said.

Do you know what 400 CHttpException mean?

Thanks

The answer is already in error message.

Your request is invalid.

invalidActionParams

here’s your action:

public function actionField($index)

Just provide required param or change action to

public function actionField($index=null)

Yes, i am newbie and thanks a lot for your help. It WORKED.

I am going to read more about Action Binding.

Regards to both you and Twisted1919