Pass key-value pair array as hidden in html form and access it

问题内容:

I am using Codeigniter- HMVC structure.
I have an array $listp in key-value pair format:
Heres what it looks like:

    array( 
     [1] => 12, 
     [3] => 33) 

I need to pass this to a view file and hide it.
Say suppose like the below format:

    <form method="post" action="<?php echo base_url().'billing/savebill'?>">
    <input type=text name="billarr" id="billarr" value="<?php print_r($listp);?>" />
    </form>

And then in Billing controller:i need to retrieve this in the same key-value format

 function savebill()
    {
         $data = array();
         $data = $this->input->post('billarr');
    }

How do i achieve this?
Thanks for the help in advance. 🙂

问题评论:

1  
convert your array to json string. then pass to input and decode in controller.
    
If you want to hide this from the end-user entirely, then consider storing in a session variable instead of hidden form fields.
1  
If you have this array before rendering view, then get this array same way after form submission.

答案:

答案1:

<form method="post" action="<?php echo base_url().'billing/savebill'?>">
    <input type=text name="billarr" id="billarr" value="<?php json_encode($listp);?>" />
    </form>

In save bill

function savebill()
    {

         $data = $this->input->post('billarr');
         $data = json_decode($data);
    }

ref: http://php.net/manual/en/function.json-encode.php

http://php.net/manual/en/function.json-decode.php

答案评论:

原文地址:

https://stackoverflow.com/questions/47753924/pass-key-value-pair-array-as-hidden-in-html-form-and-access-it

添加评论