Call PHP from JavaScript and Return an Array from PHP to JavaScript Function

Call PHP function from Javascript and return an array

you can return (echo) in the func_a.php an json string http://de.php.net/manual/en/function.json-encode.php and parse it in javascript

echo json_encode($a);

Pass a PHP array to a JavaScript function

In the following example you have an PHP array, then firstly create a JavaScript array by a PHP array:

<script type="javascript">
day = new Array(<?php echo implode(',', $day); ?>);
week = new Array(<?php echo implode(',',$week); ?>);
month = new Array(<?php echo implode(',',$month); ?>);

<!-- Then pass it to the JavaScript function: -->

drawChart(<?php echo count($day); ?>, day, week, month);
</script>

Passing a php array to a javascript array inside a PHP function

I've heard that I can translate the array in javascript by using <script>var records = <?php json_encode($records) ?></script> but it doesn't work !

That doesn't work because you didn't echo the result, you're simply running the JSON process and then discarding the result. Add an echo:

<script>var records = <?php echo json_encode($records) ?></script>

and contrary to the other answer, do not try to JSON.parse this because it is already a valid JavaScript object.

Edit: if you're trying to concatenate the JSON to a response string then don't use <?php tags, instead you can do:

$block .= '<script>var records = ' . json_encode($records) . '; </script>';

php return array into javascript

As Marc B says, the result of an SQL query is a result handle that you need to read from in order to then JSON encode:

// run the query
$result = mysql_query("SELECT * FROM table WHERE field = '{$q}'");

// fetch all results into an array
$response = array();
while($row = mysql_fetch_assoc($result)) $response[] = $row;

// save the JSON encoded array
$jsonData = json_encode($response);

In your script, use something like the following to merge that JSON into the JavaScript:

<script>
var data = <?= $jsonData ?>;
console.log(data); // or whatever you need to do with the object
</script>

Fetching PHP Data with Javascript and assign it to an array

Try this code:

function getdata(url) {
console.log('Started');
jQuery.ajax({
type: "GET",
url: "http://myserver.com/myscript.php",
dataType: "text",
error: function (xhr) {
console.log('Error',xhr.status);
},
success: function (response) {
console.log('Success',response);
}
});
}

Open the browser's console, and let me know about its contents. If you don't see Error or Success, your code isn't actually executing

how can I return array from php to javascript using ajax

PHP

echo json_encode($cars);

JavaScript

Native:

var foo = JSON.parse(xmlhttp.responseText);

With jQuery:

var foo = $.parseJSON(xmlhttp.responseText);
//or
$.getJSON("url", function(data){
//data is your array
});

UPDATE

if(xmlhttp.readyState==4 && xmlhttp.status==200){
//document.getElementById('addIO').innerHTML+=xmlhttp.responseText;
var cars = JSON.parse(xmlhttp.responseText); //cars will now be the array.
//Do whatever you want here.
$("#addIO").html(cars.join(", ")); //Join array with ", " then put it in addIO
}

If you want to use jQuery, put this in <head>:

<script type="text/javascript" src="link/to/the/file/jquery.js"></script>

Can't return array from php function to ajax request

When you moved your query logic into a function, $con, MySQL's connection object is not available. Use GLOBAL $con; inside your function.

Read this to understand Variable Scope

Method 1
Using GLOBAL keyword

function test(){
GLOBAL $con;

$query = "select * from message where seen";
$ar = [];
$res = mysqli_query($con,$query);
while($row = mysqli_fetch_array($res)){
$ar[] = $row;
}
return $ar;
}

Method 2
Pass an argument to a function

function test($con){
$query = "select * from message where seen";
$ar = [];
$res = mysqli_query($con,$query);
while($row = mysqli_fetch_array($res)){
$ar[] = $row;
}
return $ar;
}

Call it like this:

test($con);


Related Topics



Leave a reply



Submit