Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
77
rated 0 times [  81] [ 4]  / answers: 1 / hits: 18798  / 12 Years ago, wed, february 20, 2013, 12:00:00

I want to copy a PHP array into javascript array. The javascript is being used in the PHP file. It is echoed in the PHP code so when i tried to create a new array in javascripy and just assign the PHP array onto it, it was giving me an error. Array to string conversion. Could some one help me with this.



 mysql_select_db(cerebra, $con);
$sql=select name from details order by download desc limit 20;
if (!mysql_query($sql,$con))`enter code here`
{
die('Error: ' . mysql_error());
}
$query=mysql_query($sql,$con);
$names=array();
while($row=mysql_fetch_array($query))
{
$names[]=$row[0];
}


Here $names is the array i want to use in javascript



then i echo my javascript inside the php code itself..:



   function makeRequest() {
var d = document.getElementsByName(d)[0].value;
var request = gapi.client.drive.files.list();
var newnames = new Array();
newnames=$names;
request.execute(function(resp)

{
for (i=0; i<resp.items.length; i++) {
var titulo = resp.items[i].title;
var fechaUpd = resp.items[i].modifiedDate;
var userUpd = resp.items[i].lastModifyingUserName;
var userEmbed = resp.items[i].embedLink;
var userAltLink = resp.items[i].alternateLink;
var download = resp.items[i].webContentLink;
var hold=Download;
var flag=0;

<!-- var fileInfo = document.createElement('li');
<!-- fileInfo.appendChild(document.createTextNode('TITLE: ' + titulo + ' - LAST MODIF: ' + fechaUpd + ' - BY: ' + userUpd +' url: ' + hold.link(download)));
<!-- document.getElementById('content').appendChild(fileInfo);
<!--for (var i=0;i<newnames.length;i++){
<!-- if(newnames[i].toString() == titulo){
document.write(titulo + &nbsp;);
document.write(hold.link(download) + <br>);
<!--flag=1;
<!-- }
<!--}
}
<!--if(flag!=1){
<!--document.write(not found!);
<!--}
});
}


Here in the javascript i am trying to convert the php array into javascript array but its not working


More From » php

 Answers
17

Example-1:



<script type='text/javascript'>
<?php
$php_array = array('he','hi','hello');
$js_array = json_encode($php_array);
echo var javascript_array = . $js_array . ;n;
?>
</script>


NOTE: You can convert php arrays into javascript using php's json_encode() function.



Check out this convert php array to java script array for more information.



Example-2:



PHP Code:



$arr = array(1,2,3,4,5,6,7);
$script = '<script>var newArr = new Array(' . implode(',', $arr) . ');</script>';
echo $script;


HTML Code:



<script>
function doOnchange(arr){
alert(arr.length);
}
</script>
<form name=frm>
<select name=sel1 onchange=doOnchange(newArr);>
<option>1</option>
<option>2</option>
<option>3</option>
</select>
</form>


Checkout some more links,



How to Convert PHP Multidimensional Array to Javascript Object (using jQuery)



array2json() - Convert PHP arrays to JSON



json_encode pass php array to javascript



may this help you.


[#80111] Tuesday, February 19, 2013, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
freddiej

Total Points: 294
Total Questions: 95
Total Answers: 97

Location: Saudi Arabia
Member since Sat, Aug 20, 2022
2 Years ago
;