hi everyone,
I am in problem to get the array data from php to use it in js.


$data[0]=88;
$data[3]=44;
$data[4]=2232;

I want to use those data in js.


Can anyone help me ??

Recommended Answers

All 3 Replies

<?php
$data[0]=88;
$data[3]=44;
$data[4]=2232;
echo "
<html>
<head>
<script type=\"text/javascript\">
function test(){
	
alert('$data[0]');
}
</script>
</head>
<body onload=\"javascript: test();\">
</body>
</html>";
?>

This is how you can use php variables in your javascript. This is just an example.

thank you for your helping,but I need to first check the data ,If data exists then run the js code otherwise not.
this like:
first : read data from database .
second : check the data
third : if data exists then run js otherwise not.

i do it in the same file.but I want to simplify my code ,so I need to place the js code in the other file.
i just need if their have any possibility to get the array data of php in js.

You can get the JS script into some other file, but make this script use some global variable.
By this way you can parse needed array in PHP, then put the value on page and if the JS script included will find those, it will run.
It's hard code, but will work.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.