Welcome Guest, Not a member yet? Register   Sign In
Ajax Jquery : GET or POST how to catch data in controller ?
#1

[eluser]ludo31[/eluser]
Hello ;
I test my code with Ajax and I would like to know how to catch data in controller :

if we have something like this in view and in script javascript

Code:
$.ajax({
   type : 'GET', // envoi des données en GET ou POST
url : 'ajax-search.php' , // url du fichier de traitement
data : 'q='+$(this).val() , // données à envoyer en  GET ou POST
beforeSend : function() { // traitements JS à faire AVANT l'envoi
  $field.after('<img src="ajax-loader.gif" alt="loader" id="ajax-loader" />'); // ajout d'un loader pour signifier l'action
},

and We would like to send it in controller :

Code:
data : 'q='+$(this).val() ,

how to catch q ??? in GET or POST

because in simple php code we can make :


Code:
$_GET['q']

but in codeigniter I don't know if it works with input->post ???

I see also in doc the function

Code:
$this->input->is_ajax_request()
may be we must use it ???
I don't know !!!

it is the same problem I know how to call function in controller using json type but I don't now how to pass a data and cath it in controller :


Code:
[removed]
$('#getdata').click(function(){

    $.ajax({
            url: "&lt;?php echo site_url().'/ajaxcontrolleur/get_all_users';?&gt;",
            type:'POST',
            dataType: 'json',
            success: function(output_string){
                
                $("#result_table p").empty().html(output_string).show(1500);
                
                
                //    $("#result_table p").html(output_string);
                } // End of success function of ajax form
            }); // End of ajax call

});
[removed]

and in my controller

Code:
public function get_all_users(){

      
        
        
      

        echo json_encode( $this->modelajax->ajaxmodel());
        
        
    }


thanks
#2

[eluser]Stefan Hueg[/eluser]
Hi,
everything is described here: http://ellislab.com/codeigniter/user-gui...input.html

Code:
$q = $this->input->post('q'); //for post requests, q is the key
$q = $this->input->get('q'); //this is for GET requests

//or

$post_vars = $this->input->post();
$get_vars = $this->input->get();

Those function will sanitize the variables for you, so that you don't have to worry about XSS.

Passing data via AJAX is done using the data-variable inside your $.ajax function, for ex.:

Code:
$.ajax({
...
data: {
  key: 'variable',
  second_key: 'second_variable',
  q: 'my query param'
}
...
});

Try to stick to this structure and you are fine.




Theme © iAndrew 2016 - Forum software by © MyBB