表单提交后POST数据变为NULL(CodeIgniter)

dsf9zpds  于 2022-12-07  发布在  其他
关注(0)|答案(3)|浏览(257)

我在提交表单后获取发布数据时遇到了问题。我尝试做的是,当用户单击提交按钮时,存储在隐藏字段中的输入值将被赋给另一个控制器类中的变量。但是,当我尝试打印表单值时,它总是给我NULL
这是我的代码:
主计长

public function updateOrder(){
    $this->form_validation->set_rules('delivery_status_id', 'Delivery Status', 'xss_clean');
    $this->form_validation->set_rules('remarks', 'Remarks', 'xss_clean');
    $this->form_validation->set_rules('total_amt', 'Total Amount', 'xss_clean');

    $data = array('remarks' => $this->input->post('remarks'),
                  'delivery_status_id' => $this->input->post('delivery_status_id'),
                  'total_amt' => $this->input->post('total_amt'));

    if ($data['delivery_status_id'] == $getDeliveryStatusIdDelivered->row('id'))
    {
        $this->db->select('*');
        $this->db->from('user_has_penalty');
        $this->db->where("user_id =" . $this->session->userdata['id']);

        $query = $this->db->get();

        foreach ($query->result() as $row) 
        {
            $this->db->delete('user_has_penalty', array('user_id' => $this->session->userdata['id']));
            $this->db->delete('penalty', array('id' => $row->penalty_id));
        }
    }else if($data['delivery_status_id'] == $getDeliveryStatusIdCancelled->row('id'))
    {
        $penalty_amt = $data['total_amt'] / 2;
        $data = array('amount' => $penalty_amt);
        $this->penalty->insert($data);
        $penalty_id = $this->db->insert_id();

        //var_dump($penalty_id);
    }

    var_dump($this->input->post); // Outputs NULL
}

检视

...

<?php
    $name_c = 'Cancelled';
    $query = $this->DeliveryStatus->getDeliveryStatusByName($name_c);

    echo form_open('Order_Controller/updateOrder');
?>
    <input type="hidden" name="delivery_status_id" value="<?php echo $query->row('id'); ?>"/>
    <button type="submit" class="btn btn-danger">YES</button>
    <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<?php echo form_close(); ?>

代码有什么问题吗?如果有任何帮助,我们将不胜感激

gg0vcinb

gg0vcinb1#

在表单标记中,如果不指定方法,它将是get方法。

<form action="Order_Controller/updateOrder" method="post">

...

</form>
ifsvaxew

ifsvaxew2#

表单标记中缺少method

form_open('order_Controller/updateOrder', array('method'=>'post'));

注意事项:
1.请使用模型进行数据库相关查询。
1.将查询结果传递给控制器,然后传递给视图。
1.您可以在配置中添加验证。以便在一个位置处理所有验证。

x7rlezfr

x7rlezfr3#

// This Will outputs always null 
var_dump($this->input->post);
// To Get Value Array from codeigniter input class 

var_dump($this->input->post());
method will return the values in $_POST variable 

because you are accessing uninitialized property of a object
The correct way to get post variable from codeigniter input class is 

$this->input->post() is equivalent to $_POST;
$this->input->post('data') is equivalent to $_POST['data']

相关问题