jQuery使用ajax和json来切换页面按钮

时间:2016-01-19 10:29:31

标签: php jquery json ajax

大家好,我有一个带有按钮功能的JS文件,这个按钮从表格中的不同复选框中获取值。但现在我想在另一页上获得这些价值(用于发票处理)。

这是我的剧本:

$("#boutonfacturer").click(function () {

var checked = $('input[name="id_commande[]"]:checked');
var tab = [];
var jsonobj = {};
checked.each(function () {
    var value = $(this).val();
    jsonobj.value = value;
    tab.push(jsonobj);
});
         var data= { recup : tab };
         console.log(data);

        $.ajax({
                type: 'POST',
                url: 'genererfacture-facture_groupee.html',
                data: data,
                success: function (msg) {
                if (msg.error === 'OK') {
                    console.log('SUCCESS');
                } 
                else {
                    console.log('ERROR' + msg.error);
                }
        }

            }).done(function(msg) {
                console.log( "Data Saved: " + msg );
            });

});

我使用MVC架构,所以有我的控制器:

public function facture_groupee() {

    $_POST['recup'];
    var_dump($_POST['recup']);
    console.log(recup);

    $this->getBody()->setTitre("Facture de votre commande");
    $this->getBody()->setContenu(Container::loader());
    $this->getBody()->setContenu(GenererFacture::facture_groupee());

现在我的观点毫无用处。 我的代码可能犯了错误。

谢谢。

1 个答案:

答案 0 :(得分:0)

在思考之后没关系,我使用了我的ajax.php页面,这得到了我的另一个页面,感谢window.location:

我的JS:

$("#boutonfacturer").click(function () {

var checked = $('input[name="id_commande[]"]:checked');
var tab = [];
checked.each(function () {
    var value = $(this).val();
    tab.push(value);
});
var data = {recup: tab};
console.log(data);

$.ajax({
    type: 'POST',
    url: 'ajax.php?action=facture_groupee',
    data: data,
    success: function (idfac) {
        console.log("Data Saved: " + idfac);
        var id_fac = idfac;
        window.location = "ajax.php?action=facture_groupee=" + id_fac;
    }

});

});

我的php:

public function facture_groupee() {

    foreach ($_POST['recup'] as $p){
        echo $p; }