Welcome Guest, Not a member yet? Register   Sign In
How receive data in the controller sent from a view with ajax jquery.
#1

[eluser]masentinel900[/eluser]
Hello everybody.
Now I'm running with an AJAX with jquery.
I have in my view a table with the names of some tasks, I need may to show the description of selected tasks in a modal windows.

For this. I want to pass from the view to controller the "id" of the selected task.

Code:
$(document).ready(function() {
var datos = "Bonjour Diego";
var url = "modal";
var data = "";
$("#button-create-gen").click(function(){
  $.ajax({
   url  : url,
   data : datos,
   type : 'POST',
   dataType: "html",
            async: true,
   success: function(){
            alert(datos);
         },
         error: function(objeto, quepaso){
             alert("Pasó lo siguiente: "+quepaso);
         },  
   statusCode: {
    404: function() {
    alert("page not found");
    }
   }    
  });
});
});
]

This is the jquery AJAX script.. But now I want to know how can I receive the data passed with that function in my controller.

So. If I sent a string of data in the ajax funtion. HOW CAN I DO FOR LOAD THAT DATA IN A NEW VIEW?


for example.

Code:
$data = $this->get->data();
$this->load->view('new_view', $data);

where data is the data info that I sent from the before view with ajax function.

Please help me!!
#2

[eluser]CroNiX[/eluser]
Well, you're sending it as "post" in jQuery, so you'd access them with $this->input->post('post_field_name');

However your 'datos' var in jQuery should be an object of key/value pairs, not a string.

like:
Code:
var datos = {mydata: 'Bonjour Diego', moredata: 'some more data'};
And then in your controller, you'd access it with:
Code:
$mydata = $this->input->post('mydata');  // "Bonjour Diego"
$moredata = $this->input->post('moredata'); // "some more data"

So you could go:
Code:
$data = array(
  'mydata'   => $this->input->post('mydata'),
  'moredata' => $this->input->post('moredata')
);

$this->load->view('your_view', $data);
and then in your view:
Code:
echo $mydata;
echo $moredata;
as normal
#3

[eluser]masentinel900[/eluser]
Thanks by your suggest..

I have this:

[Jquery Script]
Code:
$(document).ready(function() {
var datos = {val: 'hola'};
var url = "modal";
var data = "";
$("#button-create-gen").click(function(){
  $.ajax({
   url  : url,
   data : datos,
   type : 'POST',
   dataType: "html",
   success: function(){
    $.fancybox({
    openEffect    : 'fade',
    closeMethod   : 'zoomOut',
    href          : 'modal',
    type          : 'ajax',
    helpers    : {
    title : { type : 'inside' },
                        },
     });
         },
         error: function(objeto, quepaso){
             alert("Pasó lo siguiente: "+quepaso);
         },  
   statusCode: {
    404: function() {
    alert("page not found");
    }
   }    
  });
});
});

[Controller]
Code:
$data = $this->input->post('val');
  echo $data;


But Really Is not working, I believe that like this you suggest me. Do you know tell me if I'm doing some wrong.

Many thanks by your help!!
#4

[eluser]smilie[/eluser]
Do you receive any error or?

I suggest using Firefox with Firebug, in there you can (easily) see POST you are doing;

in controller you could also do:

Code:
echo "<pre>"; var_dump($_POST); echo "</pre>";

just to be sure you are not missing something.

jquery looks good, tho' not sure about this one:

dataType: "html",

try changing it to dataType: "json";
#5

[eluser]masentinel900[/eluser]
Smilie thanks by your help.

See. if I change the html type by json. show me "parse error"
And now I'm trying to do just this ::

Code:
$(document).ready(function() {
var inputFu = $("#in-val-fun").val();
var datos = {val: inputFu};
var url = "modal";
$("#button-create-gen").click(function(){
  $.ajax({
   url  : url,
   data : datos,
   type : 'POST',
   dataType: "html",
   success: function(datos){
    alert(datos);
    /*$.fancybox({
    openEffect    : 'fade',
    closeMethod   : 'zoomOut',
    href          : 'modal',
    type          : 'ajax',
    helpers    : {
    title : { type : 'inside' },
                        },
     });*/
         },
         error: function(objeto, quepaso){
             alert("Pasó lo siguiente: "+quepaso);
         },  
   statusCode: {
    404: function() {
    alert("page not found");
    }
   }    
  });
});
});

And show me " bool[false] "..
#6

[eluser]InsiteFX[/eluser]
Should return a JSON this way it will convert to a associated array in CI.
#7

[eluser]masentinel900[/eluser]
How can do that, do you mean datatype : "json", or?
#9

[eluser]masentinel900[/eluser]
Many thanks to everyone by your suggest.

Now I made a script that send me a data from a view to CI controller. all goes ok. But I was thinking how can I do for to compare a record of a specific row with the data received from the view.

I have this:

[Jquery]

Code:
$(document).ready(function() {
var value = $("#in-val-fun");
$("#button-create-gen").click(function(){
  if($('input[name="in-val-fun"]:checked').length !== 0)
  $.post("modal", {'value' : value.val()},
  function(data){
   $.fancybox({
    openEffect    : 'fade',
    closeMethod   : 'zoomOut',
    href          : 'modal',
    type          : 'ajax',
    content       :  data,
    helpers    : {
    title : { type : 'inside' },
                        },
    });
   },"json");
  else
  {
  alert('Please check any option');
  }
});
});

[CI Controller]
Code:
function index()
{
  if($this->session->userdata('logged_in'))
  {
   $data = trim($this->input->post('value'));
   echo json_encode($data . "Diego");  
   $session_data = $this->session->userdata('logged_in');    
   $query = $this->db->get_where('funciones',array('idfuncion' => $data));
   $datas = $query->row_array();
   if($query->num_rows() > 0)
   {
    $this->load->view('Modal', $datas);
   }
   else
   {
    redirect('functionPortal', 'refresh');
   }
  }
    }

Basically all this is because I need to show all the info of a task in a new window. In my main view I have a table with the name of the tasks and a radio input, if I want to know in what consisting the task I just check the radio and send the form. then the app will show me a fancybox window with the description of the task, who will should to do it.

For that I get the value of the radio input that is the id of the task and send it to controller for may compare with the DB and like this may to send again the new data to view that will open the fancy window.

Thanks by your help!!




Theme © iAndrew 2016 - Forum software by © MyBB