0
votes

i have a form where two fields are dynamically generated through java script when a button is clicked.when the button is clicked each time,the two text field will generate again and again.now i have got the count of text field generated in a hidden field in JavaScript.How can i get the value of hiddenfield in controller and insert the values of text fields in database,by appending comma in data when the text box value is entered each time.please help me.

my javascript is

<script>
var countbox=0;
var textbox1=0;
var textbox2=0;
function getField()
{
var newtextbox1="name1"+countbox;
var newtextbox2="name2"+countbox;
document.getElementById('renderDiv').innerHTML+='<br/><input type="text" id="'+newtextbox1+'" name="'+newtextbox1+'" /><br/><input type="text" id="'+newtextbox2+'" name="'+newtextbox2+'" />';
document.getElementById('renderDiv').innerHTML+='<br/><input type="hidden" id="hiddentextField" name="hiddentextField" value="'+countbox+'" />';
countbox +=1;

}
</script>

my html code is

<input type="button" id="button1" onclick=getField();/>
<div id="renderDiv">




</div>

inside this div the two textfield is generated along with the hidden field i am not getting the value of textfield in controller while submitting and i am not getting the count of textfield.i tried like $hiddenfield=$this->input->post('hiddentextField');

2
you'l need to use ajax. research the xmlhttp request object if you're using pure javascrip - Rooster
If the fields are added inside the form, they will appear just like any other form field on the server side. What exactly is the problem? - jeroen
i am not getting the value of textfield in controller while submitting and i am not getting the count of textfield.i tried like $hiddenfield=$this->input->post('hiddentextField'); - user2169463
<input type="hidden" id="hiddentextField" name="hiddentextField" value="'+countbox+'" /> should be outside the getField() function. Other wise multiple elements with same id="hiddentextField" will be created. - Arun Unnikrishnan
The problem is am not able to access the hiddenfield count in controller.can we get the value of hiddenfield which is generated by javascript in codeigniter - user2169463

2 Answers

0
votes

you will have to post the variable inorder to pass this to the server

 <script>
    var countbox=0;
    var textbox1=0;
    var textbox2=0;
    function getField()
    {
    var newtextbox1="name1"+countbox;
    var newtextbox2="name2"+countbox;
    document.getElementById('renderDiv').innerHTML+='<br/><input type="text" id="'+newtextbox1+'" name="'+newtextbox1+'" /><br/><input type="text" id="'+newtextbox2+'" name="'+newtextbox2+'" />';
    document.getElementById('renderDiv').innerHTML+='<br/><input type="hidden" id="hiddentextField" name="hiddentextField" value="'+countbox+'" />';
    countbox +=1;
    window.location.href = "index.php?name=" + countbox-1;
//change index.php to your page name

    }
    </script>

then in the same page

<?php 

$hiddenfield=$_GET["name"];
?>
0
votes

I had the same problem before, what I did is I insert those text box inside a table, lets say, tableSample

then I use jQuery find

var _content = '';
var _findcontent = $("#tableSample");
_findcontent.find("tr").each(function(){
  $(this).find("td").each(function(){
    $(this).find("input").each(function(){
      _content += $(this).val+'~'+$(this).attr("id")+'|';
    });
  });
});

Then use ajax to pass it to your PHP

$.post("<?php echo site_url('controller_name/method_name'); ?>",
{
    content : _content
}
,function( _data){
jAlert("alert",_data,"alert");
});

In your PHP, you can use explode to get the desired values,

$content_from_page = $this->input->post("content");

$explode_string = array();
$explode_string = explode("|",$content_from_page );
$explode_arr = array()
for($x=0;$x<count($explode_string)-1;$x++)
{
  $explode_arr[] = explode("~",$explode_string[$x];
}

then print_r($explode_arr); to check

*Note: The only problem with this approach is that, if the character that is inserted into the textbox is ~ or |, coz that is the delimiter used.