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');
}
}
}