-1

I was confused how to get php made Array() value in Jquery. Please Help some one.

My Array Structure is:

Var data = Array ( [0] => Array ( [id] => 11 [date] => 2016-04-26 13:37:16 [member_id] => 46 [quantity] => 1 [costper_litre] => 40 [total_amount] => 40 [paid_amount] => 30 [balance_amount] => 10 [entry_date] => 26-04-2016 ) );

I was using to get value in jquery like this:

<script>
    var account_id = data['entry_date'];
    alert(account_id);
</script>

Error Giveing Undefined.

  • 1
    I don't quite see what is your real code. What code comes from PHP and what code comes from JQuery. – Rizier123 Apr 26 '16 at 10:23
  • 1
    You appear unsure of the split between client- and server-side code. The code you have posted is pseudo code, not actual code. Ordinarily, JavaScript would have access to server-side content either via AJAX or by the server echo'ing out that data in the source code so that JavaScript can have access to it. – Mitya Apr 26 '16 at 10:24
  • public function editoutput_entries($mid){ $conn = $this->conn; $jsonData = array(); $sql = "SELECT * FROM ".CASH_BOOK." where id=$mid"; $result=$conn->query($sql); $array = $result->fetch_assoc(); $jsonData[] = $array; return $jsonData; } – Websnap Designs Apr 26 '16 at 10:24
  • this is real code and This array come from here – Websnap Designs Apr 26 '16 at 10:25
  • so the array is a php array? where are you converting it to a javascript array? – Jester Apr 26 '16 at 10:37
  • 2
    Possible duplicate of [How to pass variables and data from PHP to JavaScript?](http://stackoverflow.com/questions/23740548/how-to-pass-variables-and-data-from-php-to-javascript) – Jester Apr 26 '16 at 10:42

5 Answers5

0

Try it:-

<?php
 $var = Array ( [0] => Array ( [id] => 11 [date] => 2016-04-26 13:37:16 [member_id] => 46 [quantity] => 1 [costper_litre] => 40 [total_amount] => 40 [paid_amount] => 30 [balance_amount] => 10 [entry_date] => 26-04-2016 );
?>



 <script>
   $( document ).ready(function() {
    Var data = <?php echo $var[]; ?>
     });
    </script>

or

<script>
    Var data = <?php print_r($var); ?>
 </script>
0

Use json_encode:

var data = <?php echo json_encode($php_array); ?>;
Dhara Parmar
  • 8,021
  • 1
  • 16
  • 27
0

try the following asuming you are using ajax;

   public function editoutput_entries($mid){ $conn = $this->conn; $jsonData = array(); $sql = "SELECT * FROM ".CASH_BOOK." where id=$mid"; $result=$conn->query($sql); $array = $result->fetch_assoc(); $jsonData['result'] = $array; return json_encode($jsonData); } 

in ajax success function:

   success:function(data){
    var account_id = data.result.entry_date;
    alert(account_id);
}
madalinivascu
  • 32,064
  • 4
  • 39
  • 55
0

You are getting undefined because entry_date key is not in data array, it's in zeroth index of data

Hence try :

  $account_id = data[0]['entry_date'];
  alert($account_id);`
AkshayP
  • 2,141
  • 2
  • 18
  • 27
0

You should read up on JSON or just google "how to pass variables from PHP to JavaScript" there is a lot of information about the multiple ways this can be done. Other StackOverflow Question

<script>
    data = JSON.parse(<?PHP echo json_encode($array); ?>);
</script>

<script>
    alert(data); //for testing purposes
    var account_id = data['entry_date'];
    alert(account_id);
</script>
Community
  • 1
  • 1
Jester
  • 1,408
  • 1
  • 9
  • 21
  • $array should be replaced by the variable you have the array saved in. You should update you question to contain a lot more information, you've given us a print_r of the php array, we need to know where you're setting it and where the jquery code is compared to the php code. it could be in 2 totally different files! Better just read up on the other question i send you. – Jester Apr 26 '16 at 10:50