在这个例子中,如何避免从视图向控制器发送变量?

时间:2013-03-01 08:34:13

标签: php javascript codeigniter

我有一个View,用于计算要编辑的条目,使用javascript删除。 id是根据勾选的复选框计算的,并存储在一个数组中,需要发送到控制器方法进行编辑或删除...我在某处读到理想情况下,变量不应该从Codeigniter中的View发送到Controller。我怎么能以不同的方式做到这一点?

查看

function checkedAll() {
    var rowlength=document.getElementById("check").rows.length; 
    z=document.getElementById("check").getElementsByTagName("input")[0].checked;

    for(var i=1;i<rowlength-1;i++)
    {
        document.getElementById("check").getElementsByTagName("input")[i].checked = z; 
    }
}

function del(){
    var rowlength=document.getElementById("check").rows.length;
    var id = new Array();

    for(var i=1;i<rowlength-1;i++)
    {
        var t = document.getElementById("check").getElementsByTagName("input")[i].checked; 
    var y = document.getElementById("check").rows[i].cells;
        id[i]=y[0].innerHTML;
    }   
}
</script>
</head>

<body>
     <table id="check" >
        <tr>
            <th>S.No</th> 
            <th>Name</th> 
            <th>Age</th> 
            <th>Qualification</th> 
            <th> <input type="checkbox" onclick='checkedAll()'/> </th>
        </tr>

        <?php 
        $check=0; 
        $flag=0;    
        $my_checkbox=array();

        foreach($forms as $ft): ?> 
            <tr id="<?php echo $check;?>" class="<?php echo $d ?>"> 
                <td > <?php echo $ft['serial']; ?> </td> 
                <td> <?php echo $ft['name'] ;?></td> 
                <td> <?php echo $ft['age'] ;?></td> 
                <td> <?php echo $ft['qualification'] ;?></td> 
                <td> <input type="checkbox" /></td>
            </tr>
            <?php $check++ ?>             
        <?php endforeach ?>

        <tr> 
            <td colspan="5" align="center"> 
                <button type="button" name="create" id='but' value="Create" 
                        onclick = "Redirect();" >Create </button>  
                <button type="button" name="edit" onclick="edit();" id='but' >Edit </button> 
                <button type="button" name="delete" id='but' onclick="del(); " >Delete </button>
            </td>
        </tr>   

    </table> 

    <form action="<?php $this->load->helper('url');echo site_url('form/edit');?>" method="POST"  id="myForm" >
        <input type="hidden" name="snap" id="snap">
    </form> 
</body>

function checkedAll() { var rowlength=document.getElementById("check").rows.length; z=document.getElementById("check").getElementsByTagName("input")[0].checked; for(var i=1;i<rowlength-1;i++) { document.getElementById("check").getElementsByTagName("input")[i].checked = z; } } function del(){ var rowlength=document.getElementById("check").rows.length; var id = new Array(); for(var i=1;i<rowlength-1;i++) { var t = document.getElementById("check").getElementsByTagName("input")[i].checked; var y = document.getElementById("check").rows[i].cells; id[i]=y[0].innerHTML; } } </script> </head> <body> <table id="check" > <tr> <th>S.No</th> <th>Name</th> <th>Age</th> <th>Qualification</th> <th> <input type="checkbox" onclick='checkedAll()'/> </th> </tr> <?php $check=0; $flag=0; $my_checkbox=array(); foreach($forms as $ft): ?> <tr id="<?php echo $check;?>" class="<?php echo $d ?>"> <td > <?php echo $ft['serial']; ?> </td> <td> <?php echo $ft['name'] ;?></td> <td> <?php echo $ft['age'] ;?></td> <td> <?php echo $ft['qualification'] ;?></td> <td> <input type="checkbox" /></td> </tr> <?php $check++ ?> <?php endforeach ?> <tr> <td colspan="5" align="center"> <button type="button" name="create" id='but' value="Create" onclick = "Redirect();" >Create </button> <button type="button" name="edit" onclick="edit();" id='but' >Edit </button> <button type="button" name="delete" id='but' onclick="del(); " >Delete </button> </td> </tr> </table> <form action="<?php $this->load->helper('url');echo site_url('form/edit');?>" method="POST" id="myForm" > <input type="hidden" name="snap" id="snap"> </form> </body>

1 个答案:

答案 0 :(得分:0)

在控制器中创建接受此数组并处理它的方法,使用jquery ajax将此数据发送到控制器...无需重新加载,直接调用没有问题。

另一方面,您可以创建直接调用的控制器方法,并在处理完数组后重定向回视图以模拟停留在那里。我不推荐这个,因为它是5-6岁的方法。

如果您需要更详细的说明,请发表评论。

以下是示例代码:

var data = //your variable to be sent
$.ajax({
    type: "POST",
    url: "/controller/method",
    data: data,
    success: function(result) {
        // data is returned by controller

    },
    error: function(){
        console.log('ERROR');
        //for errors thrown by PHP exceptions and other backend features
        //or you can return result also and fetch an exception if you go for try - catch, which I highly recommend
    }
});