dynamic textbox creation in yii

hi ,

I am now looking for creating text boxes dynamically. For example , i have a field called electricity_bill in a society. now if i enter 5 i must be able to enter 5 electricity bills of concerned blocks.

Its getting difficult to get this done. Please help me.

Thanks for your concern.

sai Rama

This is not an "simple" task… and you need good knowledge of JS/jQuery to solve this…

For this to work you would need to attach to the electricity_bill textfield event "change"… that would call (AJAX) an action with the entered value… that action would renderpartial the remaining needed input textfields…

Something like this can Help You?


<?php

$cs = Yii::app()->getClientScript();

$cs->registerScript(

  'electricity_bill_script', "


     var n_bills = $('#electricity_bill').val();

     for(i=0;i<n_bills;i++)

       $('#electricity_bill').append('<input />');


    ", CClientScript::POS_END

);

?>


<html>

    <head>

        <script language='javascript'>

            function getBoxes(){

                var val=document.getElementById("initTextBoxId").value;

                var rid=document.getElementById("boxesId");

                var str="";

                if(isNaN(val)){

                    alert("you have entered illegal value");

                    return;

                }

                else{

                    str +="<div id='allTextBoxes'>";


                    for(var i=1;i<=val;i++)

                    {

                        str +="<div>";

                        str +="    <span>new text box "+i+ " :</span>";

                        str +="    <span><input type='text' value='' size='30' /></span>";

                        str +="</div>";

                    }

                    str +="</div>";


                    str +="<div>";

                    str +="    <span><input type='button' value='Enter'  onClick='javascript:getSum()'/></span>";


                    str +="</div>";


                    rid.innerHTML=str;

                }

            }

            function getSum(){

                var txtBoxes=document.getElementById("allTextBoxes").getElementsByTagName("input");

                var sum=0;

                var val;

                for(var  i=0;i<txtBoxes.length;i++){

                    val=parseInt(txtBoxes[i].value);

                    sum +=val;

                }


                alert("total is ="+sum);

               // str+="<span><input type='text' value="sum" /></span>";

            }

        </script>

    </head>

    <body>

        <div style='padding-left:100px;padding-top:50px'>

            <span>Init textBox :</span><span><input type='text' value='' id='initTextBoxId' size='30' onblur='javascript:getBoxes()' /></span>

            &nbsp;&nbsp;

            <div id='boxesId'></div>

        </div>

    </body>

</html>

This is the code that is generating dynamic textboxes .

Now the total sum must be added to the text box.

I am not understanding how to this in yii.

Will you save only the total or even the single bills?

If you have to save many records (like 5 records of electricity bills for a single record bill) take a look at this extension and this wiki.

As said mdomba, that is not a simple task, you should be prepared to go deeper in the study of the framwework, anyway the matherial I pointed you can guide you.

I am just trying to show 5 textboxes, adding the content of the textboxes and have to show the sum in another textbox that should be updated in database table.

I created the boxes using javascript. I have to show the value in php form.(where to write the code? In controllor ?)