Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

what is the right way to pass the php array to header then post it in another file then pass it using onload event to a javascript file here is my code and it is not working

the code for the first php file

header("location: Rules.php?varFields=".http_build_query($varFields));

the code for the second php file where the php array is passed

$ddd = $_GET['varFields'];

<body onload="cmbRuleField(<?php echo $ddd;?>);" > 

the code for the external javascript file

var varDisplay = JSON.stringify(arrayyy);

and also tried this one

var varDisplay = JSON.parse(arrayyy);
share|improve this question
add comment

1 Answer 1

http_build_query generates a query string, whereas what you want is a JSON encoded string. Instead of http_build_query use json_encode:

header( 'Location: Rules.php?varFields=' . json_encode( $varFields ) );

Documentation: http://php.net/json_encode

share|improve this answer
    
do i have to do thi in this part? $ddd = json_decode($_GET['varFields']); –  user3287181 Feb 22 at 18:51
    
@user3287181 I don't know. This is your code. Do you have to do the first redirect at all? An echo $_GET['varFields']; would be fine too, to pass a JSON encoded string to the final output. –  feeela Feb 22 at 18:52
    
yup sir it is necessary cause im going to pass several php query as array variable to the javascript function for speed purposes, so like what you just said it would be like this? $ddd = json_encode($_GET['varFields']); and how about in the javascript function will it be like this? var varDisplay = JSON.stringify(arrayyy); –  user3287181 Feb 22 at 18:56
    
@user3287181 No, do not encode the data twice. You have to encode it before appending it to the URL in the redirect script. Do not use $ddd = json_encode($_GET['varFields']); – this would encode the JSON encoded data as JSON string. Just print it as it is. You don't even need to use JSON.parse in the JS, since you already passing a JSON object… –  feeela Feb 22 at 19:01
    
so i just need to pass it like this? <body onload="cmbRuleField(<?php echo $_GET['varFields']; ?>);" > –  user3287181 Feb 22 at 19:03
show 1 more comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.