Welcome Guest, Not a member yet? Register   Sign In
JQuery post parameters do not get to my controller
#1

Hi All,

I know this sounds like very similar issues already reported, but none of the solutions seems to work for me.
The parameters passed from my JQuery post (or Ajax) do not get to my controller.
I'm passing the csrf code, and also tried to disable it, but same issue.
I am fairly new to codeigniter and think I am missing something basic, but I need some guidance.

My controller is very simple:
function __construct()
{
parent::__construct();

$this->load->helper('form');
$this->load->helper('html');
$this->load->model('users_model');
$this->load->library('form_validation');
parse_str($_SERVER['QUERY_STRING'],$_POST);
}

function create()
{
var_dump($_POST);
$this->form_validation->set_rules('new_loc_loc_name', 'Address', 'required|urldecode');

if ($this->form_validation->run() == TRUE)
{
$this->users_model->filter_create();
}
else
{
$this->load->view('filters/test');
}
}

And my view also very simple:
<html>
<head>
<script src="/asset/js/external/jquery-1.11.2.js"></script>
<link rel="stylesheet" href="/asset/css/external/1.11.3-jquery-ui.css" />
<script src="/asset/js/external/jquery-ui-1.11.3.js"></script>
</head>
<body>
<?= form_open('user_filters/create', array('id' => 'form_id')); ?>
<table class="main-dialog-table">
<tr>
<td><label for="location">Location</label></td>
<td>
<input class="form_field_input" type="text" name="new_loc_loc_name" id="new_loc_loc_name"/>
</td>
</tr>
<tr>
<td>
<input type="button" value="Create" id="new_loc_add" />
</td>
</tr>
</table>
</form>
</body>

<script type="text/javascript">
$(document).ready(function(){

$('#new_loc_add').click( function() {
var url = $("#form_id").attr('action');

$.post(url,$("form#form_id").serialize());
});
});

</script>

</html>

Looking at the Network tab (using chrome), I can see the post request finished successfully:
Request URL:http://localhost/index.php/user_filters/create
Request MethodTongueOST
Status Code:200 OK

And the parameters passed seems to be correct:
csrf_test_name:af1ff53c0eef00ea08cc4407d978822c
new_loc_loc_name:aa


Further more, if I will submit the following from the browser, I can see the parameters going through:
http://localhost/index.php/user_filters/create?name=aa


Any help would be appreciated. Thanks in advance.
Reply
#2

(This post was last modified: 04-06-2015, 10:57 AM by casa.)

Perhaps put in your config file $config['csrf_protection'] = FALSE;
If its at TRUE, your are in codeigniter userguide a method with CSRF but i don't try it at this day.
link : http://www.codeigniter.com/userguide3/li...token_name

Then, you need to get your data in your controller :
PHP Code:
function create()
{
var_dump($_POST);
$this->form_validation->set_rules('new_loc_loc_name''Address''required|urldecode'); // tues on your data

if ($this->form_validation->run() == TRUE)
{
    
// get your data
    
$name $this->input->post('new_loc_loc_name'TRUE) ; // TRUE for xss_clean
    // call your method
    
$this->users_model->filter_create$name);  // you need to pass your param in your model method and build it for it
}
else
{
$this->load->view('filters/test');

Reply
#3

Thanks a lot casa.
I've tried setting csrf_protection to false, but same result.
Since this->post is still available in the model, I am retrieving all data and do processing in the model itself.
The problem is that the parameters don't even reach that stage. $_POST is always empty at the start of the method: array(0) { }
What I also don't understand is that even with csrf protection on, if I manually go to http://localhost/index.php/user_filters/create?name=aa from a browser, the following goes through fine:
array(1) { ["name"]=> string(2) "aa" }
Shouldn't this be csrf protected?

Thanks.


(04-06-2015, 10:53 AM)casa Wrote: Perhaps put in your config file $config['csrf_protection'] = FALSE;
If its at TRUE, your are in codeigniter userguide a method with CSRF but i don't try it at this day.
link : http://www.codeigniter.com/userguide3/li...token_name

Then, you need to get your data in your controller :

PHP Code:
function create()
{
var_dump($_POST);
$this->form_validation->set_rules('new_loc_loc_name''Address''required|urldecode'); // tues on your data

if ($this->form_validation->run() == TRUE)
{
 
   // get your data
 
   $name $this->input->post('new_loc_loc_name'TRUE) ; // TRUE for xss_clean
 
   // call your method
 
   $this->users_model->filter_create$name);  // you need to pass your param in your model method and build it for it
}
else
{
$this->load->view('filters/test');

Reply
#4

I just try it and i cannot found nothing wrong with the code. Is your problem still going on?

https://jsfiddle.net/h94v8qkq/
Reply
#5

Thank nekalv.

Yes, still have the problem. U also didn't think there could be something wrong with the code since it is pretty basic.
My suspicion is something to do with my environment but I have no idea what to check.
I'm pretty stuck there.



(04-07-2015, 03:47 PM)nekalv Wrote: I just try it and i cannot found nothing wrong with the code. Is your problem still going on?

https://jsfiddle.net/h94v8qkq/
Reply
#6

Have you checked in your browser's debugger to make sure jQuery is loading properly? Generally speaking, the relative paths to your js/css files in your view are likely to break on you.

If you set $config['csrf_protection'] = FALSE; as casa recommended previously, then your controller is not going to use the CSRF protection. However, if you manually input the URL with a query string into a browser, you're not using a POST, anyway, so csrf_verify() will just set a CSRF cookie and move along.
Reply
#7

Thanks mwhitney.

I've changed my jquery lib to load from google rather from locally:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
And removed all other local libraries.
I've changed the click event to call directly to the controller using full path, such that:
$('#new_loc_add').click( function() {
var url = $("#form_id").attr('action');

$.post("http://localhost/index.php/user_filters/create?name=aa");//url,$("form#form_id").serialize());
});
});
And I've disabled the csrf in the config file:
$config['csrf_protection'] = FALSE;

When submitting, I can see the request been sent successfully, but the parameters still not reaching the controller.

Request URL:http://localhost/index.php/user_filters/create?name=aa
Request MethodTongueOST
Status Code:200 OK

If I'll copy paste the URL request to the browser, the parameters do get to the controller.
Reply
#8

So I've cleaned up the code to the bare minimum. Now my controller is:
function test()
{
var_dump($_POST);
}

And my view is:

<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
</head>
<body>
</body>
<script type="text/javascript">
$(document).ready(function(){
$.post("http://localhost/index.php/user_filters/test?name=aa");

});
</script>
</html>

And csrf is disable.
The parameters still does not reach the controller.

Thanks.
Reply
#9

(This post was last modified: 04-11-2015, 10:12 AM by CroNiX.)

You're mixing a query string (get) with post. Not sure if that will work but seems odd. How do you know it doesn't reach the controller. You don't do anything in the success event of the ajax call, like display what is returned. It just goes into a black hole.

Just for giggles, try:
PHP Code:
$.post(
  
"http://localhost/index.php/user_filters/test"//url to call
  
name"aa" }, //POST data to send
  
function(data) { //callback to output data received back from controller
    
alert(data); //just alert it to see if it worked
  
}
); 
Reply
#10

Hi CroNiX,

I've put the dump at the start of the controller and I expected to see the post variables there.
I've put your code and what I get in the alert is an empty array: array(0) { }

Thanks.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB