duanha3539 2017-04-07 17:55
浏览 54

保存json post值以发送到php中的另一个函数

I have a form that when submit it send to 2 places one is php function and other to json, the reason is there is tag fields that i have to get there values is by json.

The problem is that the json array values going to different table in mysql and needs the id from the other php function. and always gave me Undefined index: elements

Code HTML:

here the form go to function cadastro_fornecedor()

<form id='form_c_fornecedor' action="<?php echo base_url().'admin/cadastro_fornecedor';?>" method="post" enctype="multipart/form-data">
   <button type="submit" class="btn btn-primary">adicionar</button>
</form>

Code JS:

here the form when submit colect the values im elements var and post them to get_value_fornecedor() and its working fine until here.

$('#form_c_fornecedor').bind('submit', function (d) {
    var elements = $('#try').tagHandler('getTags'); 
    $.post("<?php echo base_url().'admin/get_value_fornecedor/'; ?>",{elements: JSON.stringify(elements)});
});

Function get_value_fornecedor:

the function received the value with success.

public function get_value_fornecedor(){
    $elements = json_decode($_POST['elements'], true);
    return $elements;
}

Function cadastro_fornecedor:

Here that I receiving the error that Undefined index: elements thats mean the $elements value did not pass to this function.

public function cadastro_fornecedor(){
    $elements = $this->get_value_fornecedor();

    $inputall= $this->input->post();
    $this->form_validation->set_rules('nome_fantasia', 'Nome Fantasia', 'required');
    if ($this->form_validation->run() == FALSE){
      $this->session->set_flashdata('error', 'O campo Nome é obrigatório.');
    }else{
        if($_POST){
            $data_cad = date('Y-m-d');
            $data=array('desc_entidade' =>$inputall['desc_entidade'],
                        'data_cad'      =>$data_cad
                    );
            $id= $this->cake->c_fornecedor($data);

            if(!empty($elements)){
                for ($i = 0; $i < count($elements); $i++){
                    $data_produtos=array(   'fornecedor_id'    =>$id,
                                            'produto_tag'      =>$elements[$i]
                                    );
                    $this->cake->c_produto($data_produtos);
                }
            }
            $this->session->set_flashdata('success', 'O campo Nome é obrigatório.');
            //redirect(base_url().'admin/c_fornecedor');
        }
    }
}
  • 写回答

1条回答 默认 最新

  • douyi2107 2017-04-07 18:07
    关注

    Problem is you are posting data to method get_value_fornecedor but you are not saving it anywhere, make use of codeigniter session

    First you have load session library.

    $this->load->library("session");
    

    You can load it in auto load, which I think is better.

    To set session

    $this->session->set_userdata("SESSION_NAME","VALUE");
    

    To extract Data

    $this->session->userdata("SESSION_NAME");
    

    You can rewrite your function like below

    public function get_value_fornecedor()
    {
        /* This is for your ajax call for saving data inside session where key somedata*/
        if(isset($_POST['elements']))
        {
                $this->session->set_userdata("somedata",$_POST['elements']);
        }else
        {
               /* This is for accessing your saved data */
                $elements_saved = $this->session->userdata("somedata");
    
                if($elements_saved)
                {
                      $elements = json_decode($elements_saved, true);
                      return $elements; 
                }
        }
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值