Return an array with AJAX from json_encode

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Kraim88
    New Member
    • Oct 2014
    • 1

    Return an array with AJAX from json_encode

    Return an array with AJAX

    Hi, I'm new to AJAX

    I have un page where i want to call an ajax request to add something on this page.
    export.php
    Code:
    <div class="row">
        <div class="span12">
            <select id="listTable" name="listTable">
                <option value="appel">Appels</option>
                <option value="dossier">Dossiers</option>
                <option value="commande">Commandes Fournisseur</option>
             </select>
        </div>
    </div>
    
    <div class="row">
        <div class="span12">
             <button class="btn btn-primary" onClick="selectTable()">Select</button>
        </div>
    </div>
    
    <div id ="columns" class="row" style="display:none;">
        <div class="span12">
             <div id="columns-check" style="">
                <!-- Here will be displayed the content of the ajax request-->
             </div>
        </div>
    </div>
    
    <script type="text/javascript" src="_module/ExportFichier/exportFile/ajax/requestExport.js"></script>
    Here is my ajax function
    Code:
    function selectTable(table){
    
        var table = $("#listTable").val();
    
            $.ajax({
    
                url: "_module/ExportFichier/exportFile/ajax/requestColumns.php",
                type: "POST",
                data: "table="+table,
                dataType: 'json',
    
                success: function(data){
    
                    $('#columns').css('display','block');
                    $('#columns-check').empty();
    
                    for(i=0; i<data; i++){
                        $('#columns-check').prepend('<div>I would like to display here the content of my array</div>');
                    }
                },
    
                error: function(){
                    alert('The Ajax request did not works!');
                }
    
    
            });
    
    }
    requestColumns. php
    Code:
    header("content-type: application/json; charset=utf-8");
    
    require_once '../requirements.php';
    
    $tableName = $_POST["table"];
    
    $objService = new ExportFileService($tableName);
    $columns = $objService->get_columns();
    
    echo json_encode($columns);

    I didn't get the way I can return an array from my requestColumns. php file to my jquery Ajax request and after use it to modify the DOM of my page export.php.
    Thanks for your help.
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    I didn't get the way I can return an array from my requestColumns. php file to my jquery Ajax request
    but you’re already returning the array …


    use it to modify the DOM of my page export.php.
    your array is in the data variable of your success function.

    Comment

    Working...