Form Field Doesn't Appear Inside $_Post

Good morning everybody.

I have a view with two listboxes. The first is automatically filled, while the second, initially empty, is dynamically populated through three AJAX/Jquery methods (which simply copy the selected content from the first listbox to the second).

The whole form is bound to a CFormModel $model. The first listbox is paired to the $model->cqCompany field, and the second one is paired to the $model->cqCompanyCopy field.

The second listbox never appears in $_POST, and I cannot understand why.

view:


<?php Yii::app()->clientScript->registerCoreScript('jquery'); ?>


$this->layout='//layouts/column1';


$this->menu=array();

?>


<div class="form">

<?php $form=$this->beginWidget('CActiveForm', array('id'=>'cqForm')); ?>

 

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

 

    <div class="row">

        <?php

        	echo $form->label($model,'cqCompany');

         	echo $form->listBox($model, 'cqCompany', CompanyHash::getInstance()->getCompanies(),  array('empty' => array('-1'=>'ALL COMPANIES\''),'multiple' => 'multiple', 'size'=>'8')); 

	        echo "<br>";

         	echo CHtml::button('Add Selected', array('onclick'=>'add();'));

         	echo CHtml::button('Delete Selected',array('onclick'=>'delete();'));

         	echo CHtml::button('Delete All', array('onclick'=>'deleteAll();'));

		echo "<br>";

		echo $form->label($model,'cqCompanyCopy');

		echo $form->listBox($model, 'cqCompanyCopy', array(),  array('size'=>'8'));

        ?>

    </div>

 

    <div class="row submit">

        <?php echo CHtml::submitButton('Scarica'); ?>

    </div>

 

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

</div><!-- form -->




model:




<?php


/**

 * CustomQueryForm class

 */

class CustomQueryForm extends CFormModel

{

	public cqYear;

	public $cqCompany;

	public $cqCompanyCopy;


	

	/**

	 * Declares the validation rules.

	 * The rules state that username and password are required,

	 * and password needs to be authenticated.

	 */

	public function rules()

	{

		return array(

			

			array('cqCompanyCopy', 'safe'),

			array('cqYear, cqCompanyCopy', 'required'),

			

		);

	}


	/**

	 * Declares attribute labels.

	 */

	public function attributeLabels()

	{

		return array(

			'cqCompany'=>'Companies',

			'cqCompanyCopy'=>'Selected Companies',

			'cqYear'=>'Year',

		);

	}

}






in the controller there is the following action:




public function actionCustomquery(){

		$cqForm = new CustomQueryForm();

		if (isset($_POST['CustomQueryForm'])){

			$cqForm->attributes = $_POST['CustomQueryForm'];

			if ($cqForm->validate()){

				$helper = new ReportHelper($cqForm);

			 	return $helper->export();

			}

		}

		$this->render('customquery', array('model'=>$cqForm));

	}



Could you help me to understand the reason why $_POST[‘CustomQueryForm’] doesn’t contain the cqCompanyCopy field?

Thank you in advance.

Can you show your javascript code? I suspect that you’re overwriting the name attribute of the second list box with that of the first.

Hi alexjohnp,

It might be interesting to add the code


die(var_dump($_POST));

after the


if(isset($_POST...

. Or try to use elements as firebug, or the developers tools of Chrome (CTRL + Shift + i - Network tab and then submit, make sure there is no redirect in your post section)

Hi Keith, thank you for your kind reply.

Below there is my JS code.





<script type="text/javascript">

        function add(){

        	var selectedOpts = $('#CustomQueryForm_cqCompany option:selected');

        	if (selectedOpts.length == 0) {

        		alert("Please select at least company.");

        		return false;

        	}

        	$('#CustomQueryForm_cqCompanyCopy').append($(selectedOpts).clone());

        	//$(selectedOpts).remove();

			return false;


        }

        

        function delete(){

        	var selectedOpts = $('#CustomQueryForm_cqCompanyCopy option:selected');

        	$(selectedOpts).remove();

			return false;

    	}

    	

        function deleteAll(){

        	$('#CustomQueryForm_cqCompanyCopy').empty();

            return false;

    	}


</script>



Hi FOX Creation,

I still use firebug, and through it I did notice that the cqCompanyCopy wasn’t listed inside $_POST together with the other variables.

Anyway, this is the output of die(var_dump()) [two companies have been selected, id 11 and 12]:


array(2) { 'CustomQueryForm' => array(6) { 'cqCompany' => array(2) { [0] => string(2) "11" [1] => string(2) "12" } 'cqYear' => string(4) "2014" 'cqMonthStart' => string(1) "1" 'cqMonthEnd' => string(1) "1" 'cqModel' => array(1) { [0] => string(10) "MyModel.doc" } 'cqState' => string(2) "10" } 'yt3' => string(7) "Scarica" } 

Are the options in the second list box still selected? If not, they won’t be sent when the form is submitted.

What is the purpose of the second list box if all of the items within it should always be selected?

[Edit: I will stop posting, as it seems I only suggests things Keith just posts before me ;)]

Quite odd, is it possible that the .append() function copies an attribute of the cqCompany?

Thus, if you select 11 and 13, add them, and then select 7 (e.g.) what is posted, only 7?

And are the elements appended still selected?

By the way, what I noticed is that cqYear in your model has no ‘$’ in front of it, did this

happen during copying the code?