Getting Form Data Using Ajax

I am trying to get the data from this form into the relevant controller using Ajax, but it displays the Error message Undefined Index, Player

<?php

&#036;form=&#036;this-&gt;beginWidget('CActiveForm', array(


	'id'=&gt;'player-form',


	'enableAjaxValidation'=&gt;false,


)); 

?>

<div class="row">

&lt;?php echo CHtml::activeLabelEx(&#036;model,'item'); ?&gt;


&lt;?php echo &#036;form-&gt;textField(&#036;model,'item') ?&gt;

</div>

<div class="row">

&lt;?php echo CHtml::activeLabelEx(&#036;model,'currentQty'); ?&gt;


&lt;?php echo &#036;form-&gt;textField(&#036;model,'currentQty') ?&gt;

</div>

<div class="row">

&lt;?php echo CHtml::activeLabelEx(&#036;model,'requiredQty'); ?&gt;


&lt;?php echo &#036;form-&gt;textField(&#036;model,'requiredQty')  ?&gt;

</div>

<div class="row">

&lt;?php echo CHtml::activeLabelEx(&#036;model,'notes'); ?&gt;


&lt;?php echo &#036;form-&gt;textField(&#036;model,'notes')  ?&gt;

</div>

<div class="row">

&lt;?php echo CHtml::ajaxButton(&quot;Insert Item&quot;, array('player/ajaxcreate','type'=&gt;'POST'),array('success'=&gt;'allFine')); ?&gt;

</div>

Filename of the above form is list.php

public function actionAjaxCreate() {

$p = new Player();

$p->attributes=$_POST[‘Player’];

if($p->save()){

echo &quot;OK&quot;; // echo or not, if an exception dont occur then is OK.

}else{

throw new Exception(&quot;Sorry, cant create a player&quot;,500);

}

Any ideas on what is wrong here

do a var dump of your POST array and check what keys you getting in your controller

Sorry if its a bit silly but how do you see the results of var dump in yii

you can use php’s dump method, Yii is php

I did that but it doesnt display any results

&#036;var=&#036;_POST['Player'];


    echo var_dump(&#036;var);


   var_dump(&#036;var);

do a dump on the entire array, try this


echo "<tt><pre>";

var_dump($_POST);

exit;

Dear Friend

Kindly check the following

change the ajaxButton to ajaxSubmitButton.




<?php echo CHtml::ajaxButton("Insert Item",array('player/ajaxcreate','type'=>'POST'),array('success'=>'allFine')); ?>



to




<?php echo CHtml::ajaxSubmitButton("Insert Item", array('player/ajaxCreate'),array('success'=>'function(data){console.log(data)}')); ?>



Regards.

I got this result in the console

array

(

'item' =&gt; '1'


'currentQty' =&gt; '1'


'requiredQty' =&gt; '1'


'notes' =&gt; '1'

)<pre class=‘xdebug-var-dump’ dir=‘ltr’>

<b>array</b> <i>(size=4)</i>

‘item’ <font color=’#888a85’>=&gt;</font> <small>string</small> <font color=’#cc0000’>‘1’</font> <i>(length=1)</i>

‘currentQty’ <font color=’#888a85’>=&gt;</font> <small>string</small> <font color=’#cc0000’>‘1’</font> <i>(length=1)</i>

‘requiredQty’ <font color=’#888a85’>=&gt;</font> <small>string</small> <font color=’#cc0000’>‘1’</font> <i>(length=1)</i>

‘notes’ <font color=’#888a85’>=&gt;</font> <small>string</small> <font color=’#cc0000’>‘1’</font> <i>(length=1)</i>

</pre><tt><pre><pre class=‘xdebug-var-dump’ dir=‘ltr’>

<b>array</b> <i>(size=1)</i>

‘Player’ <font color=’#888a85’>=&gt;</font>

&lt;b&gt;array&lt;/b&gt; &lt;i&gt;(size=4)&lt;/i&gt;


  'item' &lt;font color='#888a85'&gt;=&amp;gt;&lt;/font&gt; &lt;small&gt;string&lt;/small&gt; &lt;font color='#cc0000'&gt;'1'&lt;/font&gt; &lt;i&gt;(length=1)&lt;/i&gt;


  'currentQty' &lt;font color='#888a85'&gt;=&amp;gt;&lt;/font&gt; &lt;small&gt;string&lt;/small&gt; &lt;font color='#cc0000'&gt;'1'&lt;/font&gt; &lt;i&gt;(length=1)&lt;/i&gt;


  'requiredQty' &lt;font color='#888a85'&gt;=&amp;gt;&lt;/font&gt; &lt;small&gt;string&lt;/small&gt; &lt;font color='#cc0000'&gt;'1'&lt;/font&gt; &lt;i&gt;(length=1)&lt;/i&gt;


  'notes' &lt;font color='#888a85'&gt;=&amp;gt;&lt;/font&gt; &lt;small&gt;string&lt;/small&gt; &lt;font color='#cc0000'&gt;'1'&lt;/font&gt; &lt;i&gt;(length=1)&lt;/i&gt;

</pre> index.php:411

Dear Friend

Let us have the controller logic like this.




public function actionAjaxCreate() 

{

    $p = new Player();

    if(isset($_POST["Player"]) && Yii::app()->request->getIsAjaxRequest())

    {

         $p->attributes=$_POST['Player'];

         if($p->save()){

             echo "You have succesfully save data";

             Yii::app()->end();

          }

         else{

             echo "Something went wrong:try again";

             Yii::app()->end();

    }

}



Then in the form…




<?php echo CHtml::ajaxSubmitButton("Insert Item", array('player/ajaxCreate'),array('success'=>'function(data){alert(data)}')); ?>



Ok I got it working now. Thanks a lot