sking5292
asked on
Passing PHP array to Javascript through XmlHttp
Hello,
I have a global javascript array and I'm trying to fill that array from a php script through Ajax but it is not working form .. appreciate your help
Here sample of the code
// ============== .js file ================//
var sel_rule = [];
function view_array(arg1){
var url="php_file.php"
url=url+"?arg1="+arg1
url=url+"&sid="+Math.rando m() // prevent using cache
swmxmlHttp=GetXmlHttpObjec t()
swmxmlHttp.onreadystatecha nge = view_array_stateChanged;
swmxmlHttp.open("GET",url, true)
swmxmlHttp.send(null)
}
functionview_array_stateCh anged(){
if (swmxmlHttp.readyState==4 || swmxmlHttp.readyState=="co mplete")
{
document.getElementById("a rray_view" ).innerHTM L= swmxmlHttp.responseText
alert(sel_rule.length); // This should show the number of elements in the array but I always get 0
}
}
// ============ php_file.php ================== //
<?
$arg1 = $_GET["arg1"];
// query database
// fill the javascrip array with query results.. I'm using fix values here
echo "<script>
sel_rule[0] = 22222
sel_rule[1] = 24444
sel_rule[2] = 22555
</script>";
?>
Thanks in advance
I have a global javascript array and I'm trying to fill that array from a php script through Ajax but it is not working form .. appreciate your help
Here sample of the code
// ============== .js file ================//
var sel_rule = [];
function view_array(arg1){
var url="php_file.php"
url=url+"?arg1="+arg1
url=url+"&sid="+Math.rando
swmxmlHttp=GetXmlHttpObjec
swmxmlHttp.onreadystatecha
swmxmlHttp.open("GET",url,
swmxmlHttp.send(null)
}
functionview_array_stateCh
if (swmxmlHttp.readyState==4 || swmxmlHttp.readyState=="co
{
document.getElementById("a
alert(sel_rule.length); // This should show the number of elements in the array but I always get 0
}
}
// ============ php_file.php ================== //
<?
$arg1 = $_GET["arg1"];
// query database
// fill the javascrip array with query results.. I'm using fix values here
echo "<script>
sel_rule[0] = 22222
sel_rule[1] = 24444
sel_rule[2] = 22555
</script>";
?>
Thanks in advance
You coud do your php like this:
echo "22222,24444,22555";
And your javascript like this:
functionview_array_stateCh anged(){
if (swmxmlHttp.readyState==4 || swmxmlHttp.readyState=="co mplete")
{
var sel_rule = swmxmlHttp.responseText.sp lit(",");
alert(sel_rule.length); // This should show the number of elements in the array but I always get 0 }
}
Also you could use JSON or XML and parse the result.
echo "22222,24444,22555";
And your javascript like this:
functionview_array_stateCh
if (swmxmlHttp.readyState==4 || swmxmlHttp.readyState=="co
{
var sel_rule = swmxmlHttp.responseText.sp
alert(sel_rule.length); // This should show the number of elements in the array but I always get 0 }
}
Also you could use JSON or XML and parse the result.
ASKER
b0lsc0tt,
Thanks for your reply,
Yes, the php code is returning that array only.
I have never used JSON before, so I did some research on how to it use to get this done, so do you think I can do this by creating JSON object in php and use javascript eval() function to excute it?
if that's not the case, I appreciate an example ..
Thank you
Thanks for your reply,
Yes, the php code is returning that array only.
I have never used JSON before, so I did some research on how to it use to get this done, so do you think I can do this by creating JSON object in php and use javascript eval() function to excute it?
if that's not the case, I appreciate an example ..
Thank you
ASKER
alexgcm,
I wish it's that simple, the problem is I'm trying to return multiple complicated arrays (hash key arrays)
sel_ruls['icmp']=[1111,222 ,333]
sel_ruls['icmp_info']=[556 1,255,443]
un_selected_ruls['dns']=[5 55,5566,66 6]
regards
I wish it's that simple, the problem is I'm trying to return multiple complicated arrays (hash key arrays)
sel_ruls['icmp']=[1111,222
sel_ruls['icmp_info']=[556
un_selected_ruls['dns']=[5
regards
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
If you want to use JSON notation:
// ============== .js file ================//
var sel_rule = null;
function view_array(arg1){
var url="php_file.php"
url=url+"?arg1="+arg1
url=url+"&sid="+Math.rando m() // prevent using cache
swmxmlHttp=GetXmlHttpObjec t()
swmxmlHttp.onreadystatecha nge = view_array_stateChanged;
swmxmlHttp.open("GET",url, true)
swmxmlHttp.send(null)
}
functionview_array_stateCh anged(){
if (swmxmlHttp.readyState==4 || swmxmlHttp.readyState=="co mplete")
{
eval("sel_rule ="+seswmxmlHttp.responseTe xt+";");
alert(sel_rule.sel_ruls.ic mp.length) ; // This should show the number of elements in the array but I always get 0
}
}
// ============ php_file.php ================== //
<?
$arg1 = $_GET["arg1"];
// query database
// fill the javascrip array with query results.. I'm using fix values here
echo "
{
{
'sel_ruls':
{
'icmp':[
1111
,222
,333
]
,'icmp_info':[
5561
,255
,443
]
}
}
,{
'un_selected_ruls':{
'dns':
[
555
,5566
,443
]
}
}
";
?>
// ============== .js file ================//
var sel_rule = null;
function view_array(arg1){
var url="php_file.php"
url=url+"?arg1="+arg1
url=url+"&sid="+Math.rando
swmxmlHttp=GetXmlHttpObjec
swmxmlHttp.onreadystatecha
swmxmlHttp.open("GET",url,
swmxmlHttp.send(null)
}
functionview_array_stateCh
if (swmxmlHttp.readyState==4 || swmxmlHttp.readyState=="co
{
eval("sel_rule ="+seswmxmlHttp.responseTe
alert(sel_rule.sel_ruls.ic
}
}
// ============ php_file.php ================== //
<?
$arg1 = $_GET["arg1"];
// query database
// fill the javascrip array with query results.. I'm using fix values here
echo "
{
{
'sel_ruls':
{
'icmp':[
1111
,222
,333
]
,'icmp_info':[
5561
,255
,443
]
}
}
,{
'un_selected_ruls':{
'dns':
[
555
,5566
,443
]
}
}
";
?>
You couls also format you strings by putting some delimeters and processin on the client. Take a look at this:
// ============== .js file ================//
var sel_rule = null;
function view_array(arg1){
var url="php_file.php"
url=url+"?arg1="+arg1
url=url+"&sid="+Math.rando m() // prevent using cache
swmxmlHttp=GetXmlHttpObjec t()
swmxmlHttp.onreadystatecha nge = view_array_stateChanged;
swmxmlHttp.open("GET",url, true)
swmxmlHttp.send(null)
}
functionview_array_stateCh anged(){
if (swmxmlHttp.readyState==4 || swmxmlHttp.readyState=="co mplete")
{
data = String(seswmxmlHttp.respon seText).re place(/\s/ g);
data = data.split(";");
for( var i=0;i<data.length;++i)
{
var k = data[i].split("^");
sel_rule[ k[0] ]=null;
sel_rule[ k[0] ][ k[1] ]=null;
sel_rule[ k[0] ][ k[1] ]= k[2].split(",");
}
alert(sel_rule.sel_ruls.ic mp.length) ; // This should show the number of elements in the array but I always get 0
}
}
// ============ php_file.php ================== //
<?
$arg1 = $_GET["arg1"];
// query database
// fill the javascrip array with query results.. I'm using fix values here
echo "
sel_ruls^icmp^1111,222,333 ;
sel_ruls^icmp_info^5561,25 5,443;
un_selected_ruls^dns^555,5 566,666;
";
?>
// ============== .js file ================//
var sel_rule = null;
function view_array(arg1){
var url="php_file.php"
url=url+"?arg1="+arg1
url=url+"&sid="+Math.rando
swmxmlHttp=GetXmlHttpObjec
swmxmlHttp.onreadystatecha
swmxmlHttp.open("GET",url,
swmxmlHttp.send(null)
}
functionview_array_stateCh
if (swmxmlHttp.readyState==4 || swmxmlHttp.readyState=="co
{
data = String(seswmxmlHttp.respon
data = data.split(";");
for( var i=0;i<data.length;++i)
{
var k = data[i].split("^");
sel_rule[ k[0] ]=null;
sel_rule[ k[0] ][ k[1] ]=null;
sel_rule[ k[0] ][ k[1] ]= k[2].split(",");
}
alert(sel_rule.sel_ruls.ic
}
}
// ============ php_file.php ================== //
<?
$arg1 = $_GET["arg1"];
// query database
// fill the javascrip array with query results.. I'm using fix values here
echo "
sel_ruls^icmp^1111,222,333
sel_ruls^icmp_info^5561,25
un_selected_ruls^dns^555,5
";
?>
It wouldn't have to be a PHP JSON object. I honestly don't know off the top of my head if PHP has one. If so it might make this easier. The key is to have the PHP response like that posted in hielo's last comment. It comes to the AJAX and Javascript just as "text" but in that style it can easily be converted by Javascript and quickly becomes an array, etc.
Let me know how that post by Hielo gets you going. I will be busy still for a few hours but can make some code, etc after that if you give me an idea of what you need. There is lots of info on JSON on the Internet too but it can be a little overwhelming. :)
bol
Let me know how that post by Hielo gets you going. I will be busy still for a few hours but can make some code, etc after that if you give me an idea of what you need. There is lots of info on JSON on the Internet too but it can be a little overwhelming. :)
bol
ASKER
eval function is all I needed
Thanks hielo
Thanks hielo
ASKER
Thank you guys, that was helpful
Does the PHP script only send that code in the response?
The problem is the script isn't being run by the browser. Even with the script tags it is just treated as html or text. If that is all the PHP script send in the "responseText" then I suggest you use JSON. I don't have time for an example of this right now but I will be happy to provide one if you need it. Basically you send the array in a little different format and then use the Javascript to get it in an array. A little more work but JSON is great. :)
Let me know if you have any questions or need more information.
b0lsc0tt