weixin_33717117 2015-06-25 09:43 采纳率: 0%
浏览 44

用ajax获取数组

I've got a problem with my ajax request. I want to send an array (handsontable) with the $.post method. So, I create and I send my array to my file like this :

var data = [["index essai",""],
        ["ID essai",""],
        ['Nom local essai',''],
        ['Nombre de traitements',''],
        ['Nombre de blocs',''],
        ['Type de rotations',''],
        ['Essai ou suivis',''],
        ['Thématique',''],
        ['Année début',''],
        ['Année dernier',''],
        ['Nom_vernaculaire',''],
        ['Nom pédagogique',''],
        ['Type de sol',''],
        ['Texture horizon surface',''],
        ['Taux éléments grossiers pcpondéral',''],
        ['Taux éléments grossiers pcvolumique',''],
        ['Taux éléments grossiers méthode inconnue',''],
        ['Statut éléments grossiers',''],
        ['Densité apparente cylindre',''],
        ['Densité apparente gamma',''],
        ['Densité apparente méthode inconnue',''],
        ['Statut densité',''],
        ['Masse terre fine',''],
        ['Type de Climat',''],
        ['Température moyenne annuelle',''],
        ['p_etp_estival',''],
        ['p_etp_septembre_mars',''],
        ['Niveau hydromorphie',''],
        ['Statut hydromorphie',''],
        ['Drainage',''],
        ['Statut drainage','']];

var container = document.getElementById('tab_essai');
var hot = new Handsontable(container, {
data: data,
minSpareRows: 1,
rowHeaders: false,
colHeaders: true,
contextMenu: true,
height: 800,
colWidths: [250,150],

});
</script>
<script type="text/javascript">
    $(document).ready(function(){
        $('#submit_button_essai').click(function(){

            $.post("ajax_insert_essai.php",{arr:data});

        });

    });

</script>

After that, I get my array in ajax_insert_essai.php with json like that :

$array = $_POST["arr"];
$json = json_encode($array);

When I try to run it, I've got an error with firebug which says : Undefined index: arr. I don't understand why because I did it once and it worked but this time, it doesn't work. Please help !

  • 写回答

1条回答 默认 最新

  • DragonWar% 2015-06-25 09:52
    关注

    You first need to convert your array into a json for example:

    $.post("ajax_insert_essai.php",{"arr": JSON.stringify(data)});
    

    And then decode the JSON in array in your PHP file (I keep your var names, but you should changes them to match with what they store):

    $array = $_POST["arr"]; // get the json
    $json = json_decode($array); //convert the json into an array
    
    评论

报告相同问题?