I need to post a json to a method in codeigniter 2.2.0, that post was made in a view with a jquery-ajax, like this:
function envia_mail_ajax(numero, form_data, ruta){
var iddiv="#mail_"+numero;
window.setTimeout(
function(){
$.ajax({
url: "<?php site_url('emailmasivo'); ?>/" +ruta+ "/" +numero,
cache: false,
type: 'POST',
data: form_data,
dataType: "json",
success: function(html){
$( iddiv ).append(html.mensaje+"\n"+'<br />');
}
});
}, 600);
}
and it was used like this (within a loop over i
):
envia_mail_ajax(i,
{para:correos_e[i],id_masivos:id_masivos_e[i],id_mat_referencia:id_mat_referencia_e[i],
id_tipouser:id_tipouser_e[i],nombre:nombres_e[i], sexo:sexos_e[i], matricula:matriculas_e[i], passa:passa_e[i],id_cuenta:cuenta_id},
"<?php echo $r_ajax; ?>");
now, I´m writing all that in such a way that no view will be needed, in order to make it possible to run it from the terminal´s commands line, essentially, this is telling me to POST to the controller´s method "<?php echo site_url('emailmasivo'); ?>/" +ruta+ "/" +numero
the data in form_data; to do this I wrote a method based in a lecture I found here POST json to PHP, my method is:
function procesaInfo($Arreglo, $numero){
$url = $Arreglo['r_ajax'];
$ch = curl_init(echo site_url('emailmasivo') . $url . "/" . $numero);
$jsonData = array(
'para' => $Arreglo['lista_mails']['correos_e'][$numero],
'id_masivos' => $Arreglo['lista_mails']['id_masivos_e'][$numero],
'id_mat_referencia' => $Arreglo['lista_mails']['id_mat_referencia_e'][$numero],
'id_tipouser' => $Arreglo['lista_mails']['id_tipouser_e'][$numero],
'nombre' => $Arreglo['lista_mails']['nombres_e'][$numero],
'sexo' => $Arreglo['lista_mails']['sexos_e'][$numero],
'matricula' => $Arreglo['lista_mails']['matriculas_e'][$numero],
'matriculas_e' => $Arreglo['lista_mails']['passa_e'][$numero],
//'id_cuenta' => $Arreglo['lista_mails']['cuenta_id'][$numero]
'id_cuenta' => $Arreglo['id_cuenta']
);
$jsonDataEncoded = json_encode($jsonData);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonDataEncoded);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
$result = curl_exec($ch);
}
the problem is that when I try to use that method like this:
function proceso_envia($nuevoArreglo){
$hola = "hola";
//echo $cuenta_id = $nuevoArreglo['id_cuenta'].PHP_EOL;
//print_r($nuevoArreglo['lista_mails']['correos_e']);
if(count($nuevoArreglo['lista_mails']['correos_e']) != 0){
$j = 0;
for($i = $nuevoArreglo['ini'] ; $i < count($nuevoArreglo['lista_mails']['correos_e']) ; $i++){
if($nuevoArreglo['lista_mails']['correos_e'][$i] != NULL){
$j = $j+1;
sleep(1);
echo "si llega!".PHP_EOL;
$this->procesaInfo($nuevoArreglo, $i);
}
}
}
}
it seems that no data are being POST to my method, and worst, not even the method is being reached, how do I know? well, I used an echo "I´m here!";
at the very beginning of the proceso_envia
function, and nothing was displayed... am I doing it right? how do I post correctly data to a CI method in a controller? thanx i.a. for your help!