save the json post value to send to another function in php -
i have form when submit send 2 places 1 php function , other json, reason there tag fields have there values json.
the problem json array values going different table in mysql , needs id other php function. , gave me undefined index: elements
code html:
here form go 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 form when submit colect values im elements var , post them get_value_fornecedor() , 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 value success.
public function get_value_fornecedor(){ $elements = json_decode($_post['elements'], true); return $elements; } function cadastro_fornecedor:
here receiving error undefined index: elements thats mean $elements value did not pass 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)){ ($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'); } } }
problem posting data method get_value_fornecedor not saving anywhere, make use of codeigniter session
first have load session library.
$this->load->library("session"); you can load in auto load, think better.
to set session
$this->session->set_userdata("session_name","value"); to extract data
$this->session->userdata("session_name"); you can rewrite function below
public function get_value_fornecedor() { /* ajax call saving data inside session key somedata*/ if(isset($_post['elements'])) { $this->session->set_userdata("somedata",$_post['elements']); }else { /* accessing saved data */ $elements_saved = $this->session->userdata("somedata"); if($elements_saved) { $elements = json_decode($elements_saved, true); return $elements; } } }
Comments
Post a Comment