[SOLVED]secret question tip

Hi, I need some tips/suggestions from all of you.

currently I have a secret question input field to fill-in by a user who wish to register.

then I was asked to put a pre-made questionaires , i was thinking of creating a drop down

this is what i currently have




<div class = "row">

	<?php echo CHtml::activeLabelEx($form,'SecretQuestion'); ?>

	<?php echo CHtml::activeTextArea($form,SecretQuestion', array('rows' => 2,'cols' =>15)); ?>

</div>



because of the requested additional stuff, I created a function at the members model (yii, i learned it from the book ;D )




	public function getSecretQuestions()

	{

		return array(

			0 => 'What is your favourite Colour?',

			1 => 'What is your mothers Maiden Name?',

			2 => 'What is your Pets Name?',

			3 => 'What was your first telephone number?',

			4 => 'What was your favourite teachers name?',

			5 => 'What is the title of your favourite film?',

			6 => 'What is the name of your favourite book?',

		);

	}



but how’s/what’s the best way to approach this thing ?,

  1. what if the user doesn’t want a pre-made question ?
  • i have seen this kind of stuff in some websites when I use to register
  1. is there a yii function/component that can help me with this ?

here’s what i have right now at the view file




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

<script>

	$(document).ready(function(){

		$('#textSecret').css('display','none');

		$('#ownSecret').click(function(){

			$('#dropSecret').hide();

			$('#textSecret').show();

		});

	});


</script>




<div id="dropSecret" class="row">

	<?php echo CHtml::activeLabelEx($form,'WSMembershipSecretQuestion'); ?>

	<?php echo CHtml::activeDropDownList($form,'WSMembershipSecretQuestion',$form->getSecretQuestions()); ?>

</div>


<?php echo CHtml::link('write my own secret question','#',array('id'=>'ownSecret')); ?>


<div id="textSecret" class = "row">

	<?php echo CHtml::activeLabelEx($form,'WSMembershipSecretQuestion'); ?>

	<?php echo CHtml::activeTextArea($form,'WSMembershipSecretQuestion', array('rows' => 2,'cols' =>15)); ?>

</div>



my question is, how can i make it sure which of the form field will get save in the db once i submitted the form ?. Even if I hid one of them using the jquery, isn’t it still part of the $_POST ? , e.g the array in the drop down list of question …

this is the problem I am having now

it doesn’t recognize the question in my drop down menu?

any ideas how to fix this?,

try to add it to the ‘safe’ variables in your model’s rules.

I tried that, but it didn’t helped, I var_dumped the things that are being grabbed by

$_POST[] array,

i got this





'WSMembershipSecretQuestion' => string '' (length=0)




it grabs the text area and not the dropdown array list , that’s why

the error is occuring, any other idea ?

You have to save the text area in a different field, like "alternative_secret_question".

You can add a new alternative:




        public function getSecretQuestions()

        {

                return array(

                        0 => 'What is your favourite Colour?',

                        1 => 'What is your mothers Maiden Name?',

                        2 => 'What is your Pets Name?',

                        3 => 'What was your first telephone number?',

                        4 => 'What was your favourite teachers name?',

                        5 => 'What is the title of your favourite film?',

                        6 => 'What is the name of your favourite book?',

                        7 => 'Write my own secret question',

                );

        }



And with jquery you can show the field only if is selected the 7th options.

I got a few questions with regards to your suggestion

  1. I have this WSMembershipSecretQuestion table column set as ‘not null’,

that supposed to save the written secret question, if I’ll add

a new table column named e.g alternative_secret_question,

how will I implement that?, if only one of them should be saved and one is hidden ?

apparently, my form grabs the hidden textarea’s values and not the dropdown selection :(

problem solved, i changed my jquery to this




<script>

	$(document).ready(function(){

		$('#textSecret').hide();

		$('#ownSecret').click(function(){

			$('#dropSecret').remove();

			$('#textSecret').show();

			$ ('#ownSecret').hide();

		});

	});


</script>



problem is beacuse you have to setup first atribute

this is the solution , tested and works:

CHtml::dropDownList(‘WSMembershipSecretQuestion’,0,$form->getSecretQuestions);

The trick is to add a new field in database, WSMembershipMySecretQuestion.

In case of MySecretQuestion, in database there will be saved

WSMembershipSecretQuestion= 7

WSMembershipMySecretQuestion= ‘what is…’

If you remove the dropdown, a user cannot change his choice, and that is not the expected behavior.

The solution is simply to add a new field in database.

ok I’ll try this one too., but can you give a jquery snippet for it, like how to select from the array in the drop down ? all i can do for now is some basic select and some magic hide and show :D

am using an activeDropDownList




	<?php echo CHtml::activeDropDownList($form,'WSMembershipSecretQuestion',$form->getSecretQuestions()); ?>



it won’t work if am gonna remove the “$form” , or it will cause an error if I’ll add zero at the left side of $form->getSecretQuestions()

this is the final version i have




$(document).ready(function(){

	$('#textSecret').hide();

	$('#showDrop').hide();

	$('#drop').change(function(){

		if($(this).val() == 7){

			$('#textSecret').show();

			$('#showDrop').show();

			}

		else{

			$('#textSecret').hide();

			}

		});	

});






I added the extra column in my db table to place the value of the text area

once the 7th key value pair is selected from the dropdown.

works like a charm ;D , thanks for the suggestions