How to make the view that I am loading in the controller run

1

I have this function in my view

 $.ajax({<br>
     type: "POST",            
     url: url, <br>
     data: {id: id}, 
})

Where url contains an address to the controlled I am using Codeigniter now in the controller I have

if ($this->input->is_ajax_request())
{
   $data['datos'] = $this->input->post('id');
   $this->load->view('add_cliente_view',$data);
}

The code is executed correctly (I see it in the Firebug ) but it does not change to the view I am indicating.

    
asked by carlos 04.11.2016 в 21:09
source

1 answer

1

I think you are looking for security by sending the ID by post, I show you 3 ways to do what you want (2 with get and 1 with post):

1st with get) instead of $ .ajax uses:

window.location.href = url + id;
// donde url sería: la url base + "controlador/add_cliente_view/id"

2nd with get) change in the controller

if ($this->input->is_ajax_request())
{
   $id = $this->input->post('id');
   redirect('controlador/add_cliente_view/'.$id); // por url
}

3ra) when wanting to get to a url with post, you must simulate a submit

// Esta función en javascript te ayudará a llegar a una url con POST simulado.
function post(url, parametros) { // parametros es json
    var form = $('<form></form>');

    form.attr("method", "post");
    form.attr("action", url);

    $.each(parametros, function(clave, valor) {
        var campo = $('<input></input>');

        campo.attr("type", "hidden");
        campo.attr("name", clave);
        campo.attr("value", valor);

        form.append(campo);
    });

    $(document.body).append(form);
    form.submit();
}

and to execute the function in javascript you write:

post(url + 'controlador/add_cliente_view', {id: id});
// url sería base_url(), no sé que tienes en url

Finally, where you got your problem, it would be useful for you in a div of your same page, you can load the form, but as you need to change url it would be these 3 forms

$.ajax({<br>
    type: "POST",            
    url: url, <br>
    data: {id: id}, 
}).done(function(data) {
    $('#nombre_div').html(data);
});

See you

    
answered by 05.11.2016 в 01:09