panique/mini3

Post a form in Ajax

Closed this issue · 6 comments

Hello community,

I have only recently started working with Mini and cannot send and then process the data transmitted via a form.
Can someone help me set up this script?

thank you in advance.

What have you tried? How is your script looking right now? What isn't working? Any errors?

I have two select boxes in a form.
I would like the second to display different items when the value of the first changes.
The example with Ajax shows how to send the url but not the variables.
If possible, please show a model where we send the url, variables in post method.

Sorry for my English, I'm french man.

What have you tried? How is your script looking right now? What isn't working? Any errors?

I want something that looks like this :

$('.category_list').change(function(){
$.ajax({
url + "/banner/ajaxGetNews",
data:"category = " +category,
type:"POST"

})
.done(function(result) {
$('.news_list').html(result);
})
.fail(function() {
// this will be executed if the ajax-call had failed
})
.always(function() {
// this will ALWAYS be executed, regardless if the ajax-call was success or not
});
});

data:"category = " +category,
type:"POST"

I assume your variable category is defined globally? Other than that your JS looks fine.

Next you should have a BannerController in your application\Controller directory. Something like this should work fine:

<?php

namespace Mini\Controller;

class BannerController {
    public function ajaxGetNews() {
        echo json_encode($_POST);
    }
}

this example should return your AJAX POST-Request as a JSON object.

data:"category = " +category,
type:"POST"

I assume your variable category is defined globally? Other than that your JS looks fine.

Next you should have a BannerController in your application\Controller directory. Something like this should work fine:

<?php

namespace Mini\Controller;

class BannerController {
    public function ajaxGetNews() {
        echo json_encode($_POST);
    }
}

this example should return your AJAX POST-Request as a JSON object.

no, my category variable is not global.
if i complete the script like this, do you think it will work?

$('.category_list').change(function(){
var category = $(this).val();
$.ajax({
url + "/banner/ajaxGetNews",
data:"category = " +category,
type:"POST"
})
.done(function(result) {
$('.news_list').html(result);
})
.fail(function() {
// this will be executed if the ajax-call had failed
})
.always(function() {
// this will ALWAYS be executed, regardless if the ajax-call was success or not
});
});

That should work, as long your element .category_list is a form input. Also, why aren't you just testing it :)