0

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');

4

2 回答 2

0

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"];
?>
于 2013-08-10T16:10:17.177 回答
0

我之前遇到过同样的问题,我所做的就是将这些文本框插入到表格中,比如说,tableSample

然后我使用 jQuery 查找

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")+'|';
    });
  });
});

然后使用 ajax 将其传递给您的 PHP

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

在您的 PHP 中,您可以使用 explode 来获取所需的值,

$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];
}

然后 print_r($explode_arr); 去检查

*注意:这种方法的唯一问题是,如果插入到文本框中的字符是 ~ 或 |,因为这是使用的分隔符。

于 2013-08-12T00:54:42.900 回答