保存链接分配值以及转到其他页面

save link assign value as well as go to other page

本文关键字:其他 链接 分配 保存      更新时间:2023-09-26

我有一个表格,用户可以在其中添加数据,我也有输入字段和保存链接。在我的保存链接中,如果我单击它..我在输入字段转到其他页面之前为其分配值。但是当它转到另一个页面并且我回显输入字段的值时,我得到了空,就像空值一样。

这是我的代码:对于表:

<table class="table " id="memberTB">
    <thead>
        <tr>
            <th >First Name</th>
            <th >Middle Name</th>
            <th>Last Name</th>
        </tr>
    </thead>
    <tbody>
        <tr id="first">
            <td><span class="edit"></span></td>
            <td><span class="edit"></span></td>
            <td><span class="edit"></span></td>
        </tr>
    </tbody>
    <button type="button" class="btn btn-link" id="addrow">
        <span class="fa fa-plus"> Add new row</span>
    </button>
</table>
<input type="text" name="list" id="list"/>
<br>
<a class="btn" id="savebtn">Save</button> 
<a href="#" class="btn" id="resetbtn">Reset</a>

对于 JS:

$('#savebtn').click(function() {
    var cells = 3; //number of collumns
    var arraylist = []
    var x=0;
    $('tbody tr',$('#memberTB')).each(function(){
        var cell_text = '';
        for(var i = 0 ; i < cells ; i++){
            if(i==2){
                cell_text =cell_text+$(this).find('td').eq(i).text()+":";   
            }else{
                cell_text =cell_text+$(this).find('td').eq(i).text()+",";
            } 
        }
        arraylist.push(cell_text);
    });
    document.getElementById("list").value =arraylist;
    document.getElementById("savebtn").href="<?php echo site_url('test/save');?>";   
}

当我在测试中回显它时.php在save()中,我什么也没得到,就像这样:

echo $this->input->post('list');

您只是在重定向页面。因此,您将不会从帖子中获得任何价值。如果要通过发布方法获取值,则需要使用表单提交值。

 <form id='save_form' method="post" action="<?php echo site_url('test/save');?>">   
    //add your html codes 
    //<table ..... and others
    <a class="btn" href="#" id="savebtn">Save</button> //add # to href for save button
</form>

现在你的 js

$('#savebtn').click(function() {
//js codes that you wrote
//just replace the following line 
//document.getElementById("savebtn").href="<?php echo site_url('test/save');?>";   
 $('#save_form').submit();
}
相关文章: