Welcome Guest, Not a member yet? Register   Sign In
Problem sending data from ajax to controller
#1

[eluser]bigmac90[/eluser]
Hey Guys

I wonder if anyone can help me. I'm trying to send multiple inputs from the view to my controller from a .js file using the $.ajax function. However, the only problem is my data isn't being going over to the controller.

My JS Code:

Code:
$('#bttnSubmit').click(function() {
   // Gather the input from the view and place them into variables
   var company = $('#client :selected').val();
   var dateFrom = $('#dateFrom').val();
   var dateTo = $('#dateTo').val();

   var postData = {'company' : company, 'dateFrom' : dateFrom, 'dateTo' : dateTo};

   $.ajax ({
    type:"POST",
    url:"http://localhost/ProjectSage/index.php/site/ajax_test",
    data: postData,
    cache: false,
    success: function(data) {
     alert(data);
    }
   }); // End of $.AJAX
  }); // End of click function

My Controller Code

Code:
function ajax_test() {
  $company = $_POST['company'];
  $dateFrom = $_POST['dateFrom'];
  $dateTo = $_POST['dateTo'];

  echo "IT WORKS";
}

Does anyone know whats wrong??
#2

[eluser]CroNiX[/eluser]
What happens when you manually go to http://localhost/ProjectSage/index.php/site/ajax_test?
#3

[eluser]bigmac90[/eluser]
I keep getting three error messages saying something like undefined $company, etc. With an echo "It Works".
#4

[eluser]CroNiX[/eluser]
Try using CI's input::post() method instead of using the global $_POST, which get's sanitized.

So what does your console show when the ajax request is sent?
#5

[eluser]ivantcholakov[/eluser]
@bigmac90

Paste html code of the form.
#6

[eluser]bigmac90[/eluser]
hey guys

thanks for the replys. @CroNiX, tried using the CIs input::post and still got the same result. @ivantcholakov, my code for the form looks like:

Code:
echo form_open();

                echo form_label('Client:', 'client');
                echo form_dropdown('clients', array('' => '--ALL--') + $clients, '', 'id = "client"');

                echo form_label('From:', 'from');
                echo "<input type = 'text' class = 'datepicker' name = 'dateFrom' id = 'dateFrom' />";

                echo form_label('To:', 'to');
                echo "<input type = 'text' class = 'datepicker' name = 'dateTo' id = 'dateTo'  />";

                echo "<p></p>";

                echo form_submit('submit', 'Get Table', 'id = "bttnSubmit"');
                echo form_close();




Theme © iAndrew 2016 - Forum software by © MyBB