By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,116 Members | 1,097 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,116 IT Pros & Developers. It's quick & easy.

Return an array with AJAX from json_encode

P: 1
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
Expand|Select|Wrap|Line Numbers
  1. <div class="row">
  2.     <div class="span12">
  3.         <select id="listTable" name="listTable">
  4.             <option value="appel">Appels</option>
  5.             <option value="dossier">Dossiers</option>
  6.             <option value="commande">Commandes Fournisseur</option>
  7.          </select>
  8.     </div>
  9. </div>
  10.  
  11. <div class="row">
  12.     <div class="span12">
  13.          <button class="btn btn-primary" onClick="selectTable()">Select</button>
  14.     </div>
  15. </div>
  16.  
  17. <div id ="columns" class="row" style="display:none;">
  18.     <div class="span12">
  19.          <div id="columns-check" style="">
  20.             <!-- Here will be displayed the content of the ajax request-->
  21.          </div>
  22.     </div>
  23. </div>
  24.  
  25. <script type="text/javascript" src="_module/ExportFichier/exportFile/ajax/requestExport.js"></script>
  26.  
Here is my ajax function
Expand|Select|Wrap|Line Numbers
  1. function selectTable(table){
  2.  
  3.     var table = $("#listTable").val();
  4.  
  5.         $.ajax({
  6.  
  7.             url: "_module/ExportFichier/exportFile/ajax/requestColumns.php",
  8.             type: "POST",
  9.             data: "table="+table,
  10.             dataType: 'json',
  11.  
  12.             success: function(data){
  13.  
  14.                 $('#columns').css('display','block');
  15.                 $('#columns-check').empty();
  16.  
  17.                 for(i=0; i<data; i++){
  18.                     $('#columns-check').prepend('<div>I would like to display here the content of my array</div>');
  19.                 }
  20.             },
  21.  
  22.             error: function(){
  23.                 alert('The Ajax request did not works!');
  24.             }
  25.  
  26.  
  27.         });
  28.  
  29. }
requestColumns.php
Expand|Select|Wrap|Line Numbers
  1. header("content-type: application/json; charset=utf-8");
  2.  
  3. require_once '../requirements.php';
  4.  
  5. $tableName = $_POST["table"];
  6.  
  7. $objService = new ExportFileService($tableName);
  8. $columns = $objService->get_columns();
  9.  
  10. 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.
Oct 17 '14 #1
Share this Question
Share on Google+
1 Reply


Dormilich
Expert Mod 5K+
P: 8,639
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.
Oct 18 '14 #2

Post your reply

Sign in to post your reply or Sign up for a free account.