Accessing textfield input from Controller

Hello all,

Been lurking around here for a good month or so now and just hit a wall in trying something different with a database call and AJAX.

I have a view which is renderPartial’d. This view contains display information for records, rendered from a full view and calling the actionUpdateAjax() in the controller.

This is the code:


public function actionUpdateAjax()

        {

            $this->renderPartial('partials/inStock/acc01', array(

            'Assets' => Assets::model()->findAllByPk(65), false, true));

         }

Which runs just fine. My problem is that I want to do this with a variable in place of the 65. This variable would be inserted in a view as shown here:


echo CHtml::textField('Text', 'some value', 

                        array('id'=>'searchInput'));

echo CHtml::ajaxButton('Search:',

                        array('UpdateAjax'),

                        array('update'=>'#data1'))

The above code - including the ajaxButton but not the textField - works as it should. What I wish to do is something like this in the controller:


public function actionUpdateAjax()

        {   

            $searchParam = $_POST['searchInput']; //i.e. this would grab the data from the CHtml::textField


            $this->renderPartial('partials/inStock/acc01', array(

            'Assets' => Assets::model()->findAllByPk($searchParam), false, true));

         }

Any one got any ideas what I should do instead?

Thanks for any help, this really has me stumped.

Can no one offer tips on this one?

Replying to your own posts is not a good idea! It makes people think it might have been answered.

With regards to your problem, the only ways to send back field values is 1) to do it automatically when you post the form (as per your suggestion of using $_POST) and 2) To manually send the values back using javascript. I don’t know exactly what is rendered with your ajax button but you will need it to call a javascript function instead of whatever it plumbs in by default and get that javascript function to call the Ajax function and pass it document.getElementById(‘searchbox’).value

Not sure if there is any special way in Yii to do that but I doubt it.

Put this in your view:




$url = 'path/to/action/UpdateAjax';


echo CHtml::textField('Text', 'some value', array('id'=>'searchInput'));

echo CHtml::ajaxSubmitButton('Search',$url,array('type'=>'post','update'=>'#data',));



Controller code is OK.

I’ve added the code example (thanks btw) but I’m having the same problem. I had tried adding type=>post before and got nothing. I’m sure the action is running because I have it producing other effects just to test.


        public function actionUpdateAjax()

        {

            ///These lines are just for testing, I get the same results without

            $searchParam = "helloWorld";

            echo $searchParam;

            $searchParam = $_POST['searchInput'];

            echo $searchParam;

            ////////


            $this->renderPartial('partials/inStock/acc01', array(

            'Assets' => Assets::model()->findAll('assetID=:assetID', array('assetID'=>$searchParam))));

         }

This should produce the effect I’m looking for but instead all I get is helloWorld.

This seems weird to me since this is how it should work and it just doesn’t. :confused:

Some check points:

  1. hope there is a div or something with the id="data" to update.

  2. Comment all code within actionUpdateAjax() and put print_r($_POST); check in firebug if getting the value in $_POST[‘searchInput’]. if its there that means the problem is in your findAll code or render code.

  3. put the findAll() code in a variable & check whether its getting data or not? If working pass the var to the renderPartial. Also check ur view path is correct.

like:


$model = Assets::model()->findAll('assetID=:assetID', array('assetID'=>'2'));


$this->renderPartial('partials/inStock/acc01', array('Assets' => $model));

check out…

Well the div is there and is updating fine with the partialRender. I know that the code - including the findAll - works since I can use it as intended when I hard-code the parameter. If you look at the last code I supplied I have stripped the process back to find the missing link and it is definitely the $_POST command.

When I do this:


            $searchParam = "helloWorld";

            echo $searchParam;

            $searchParam = $_POST['searchInput'];

            echo $searchParam;



I find that it returns


helloWorld

but then blank.

Seems really weird to me. It’s like it is using the $_POST since it clears out the variable but is not really finding the content of the $_POST’d textbox.

oops, sorry i just missed out the data option last time.

Here is the updated code. successfully tested and this time it should work for u.


$url = 'path/to/action/UpdateAjax';


echo CHtml::textField('Text', 'some value', array('id'=>'searchInput'));

echo CHtml::ajaxSubmitButton('Search', $url, array(

                'type'=>'post',

                'update'=>'#data',

                'data'=>"js:{searchInput: $('#searchInput').val()}"));

Amazing. That was exactly what I was looking for. I wasn’t expecting it to be done through JavaScript though. How come the usual $_Post doesn’t work?

I’m immensely grateful :)