23733

how to pass a php array to javascript array using jquery ajax?

Question:

I followed other reslted question but still unable to solve this problem. I want to store the values of an array from <strong><em>php</em></strong> into an array of <strong><em>js</em></strong>. I tried myself butr getting indefined value in all the cases i tried Plese anyone let me know where i am wrong my Php code

<?php $var=5; $myArray = array(); while($var<10){ $myArray[]=$var; $var++; } echo json_encode($myArray); ?>

and the js code

jQuery(document).ready(function(){ jQuery("#previous").click(function(){ var res = new Array(); var i= 0; jQuery.getJSON("phparray.php", function(data) { while(i<5){ res[i]=data.i; i++; } }); }); jQuery("#result").html(res[0]); });

also treid this js

jQuery(document).ready(function(){ jQuery("#previous").click(function(){ var res = new Array(); var i= 0; jQuery.getJSON("phparray.php", function(data) { jQuery(data).each(function(key, value) { res[i]=value; i++; }); }); jQuery("#result").html(res[0]); });

Answer1:

Try below code

<?php $var=5; $myArray = array(); while($var<10){ $myArray[]=$var; $var++; } $dataarray=array("myarray"=>$myArray); echo json_encode($dataarray); ?>

<strong>Jquery</strong>

jQuery(document).ready(function(){ jQuery("#previous").click(function(){ var res = new Array(); jQuery.getJSON("phparray.php", function(data) { var i= 0; while(i<data.myarray.length){ res[i]=data.myarray[i]; i++; } jQuery("#result").html(res[0]); }); }); });

Answer2:

The problem with your code is you are updating the result before the JSON has been loaded. There is also no reason to copy every item in the array in this case just set res = data (although the above example of sending back an associative array or JS object is good practice).

PHP

<?php for($var=5; $var<10; $var++){ $myArray[]=$var; } echo json_encode($myArray);

JavaScript

$(document).ready(function() { var res; $("#result").bind('update', function() { $("#result").html(res[0]); }); $("#previous").click(function(){ $.getJSON("phparray.php", function(data) { res = data; $("#result").trigger('update'); }); }); });

Recommend

  • What needs finalization in a multidimensional dynamic array?
  • Stored data can't access in Yii
  • Is there a need to prepend setTimeout and setInterval with window object?
  • Is there a MySQL query that can encode to JSON in exactly this way?
  • FCM on app closed
  • running median of constant size array
  • Html select multiple get all values at onchange event
  • Stop Bash Script if Hive Fails
  • How to add git credentials to the build so it would be able to be used within a shell code?
  • Problem while Building a Setup Project for a windows Service?
  • Detect when Facebook like button is clicked
  • Keep this build forever option - Jenkins
  • preg_replace Double Spaces to tab (\\t) at the beginning of a line
  • dc-js disable selecting slices on click for pie chart
  • Meteor: Do Something On Email Verification Confirmation
  • How to clear text inside text field when radio button is select
  • Why ng-show works with ng-repeat but ng-if doesn't? [duplicate]
  • Updated Ionic CLI but shows previous version (Windows)
  • Display Images one by one with next and previous functionality
  • Upload files with Ajax and Jquery
  • jquery mobile loadPage not working
  • Data Validation Drop Down Box Arrow Disappearing
  • How to delete a row from a dynamic generate table using jquery?
  • json Serialization in asp
  • Acquiring multiple attributes from .xml file in c#
  • using HTMLImports.whenReady not working in chrome
  • How do you join a server to an Active Directory (domain)?
  • How to CLICK on IE download dialog box i.e.(Open, Save, Save As…)
  • embed rChart in Markdown
  • How to get Windows thread pool to call class member function?
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • Authorize attributes not working in MVC 4
  • How can I remove ASP.NET Designer.cs files?
  • EntityFramework adding new object to nested object collection
  • costura.fody for a dll that references another dll
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • UserPrincipal.Current returns apppool on IIS
  • Conditional In-Line CSS for IE and Others?
  • java string with new operator and a literal