Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

im trying to pass a php array to javascript function onload that will display the js array in a drop down list but now im already doing it for sometime i guess i need to pop it again

first i pass it from one php file to another using this code

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

secondly i transfer to another variable as it had been passed to the said php file

<?php
$varArray = unserialize($_GET['varFields']);
?>

third part is im tyring to pass it into a jS functon that will then display it to a drop down list

<body id="body"  onclick="cmbRuleField(\'' + <?php echo json_encode($varArray);?> + '\');"    >

and here is the external javascript code

function cmbRuleField(varArray)//ruleField 
{   
    var varDisplay = JSON.stringify(varArray);

        var sel = document.getElementById("ruleField") // find the drop down

        for (var i in varDisplay) 
        { // loop through all elements

            var opt = document.createElement("option"); // Create the new element
            opt.value = varDisplay [i]; // set the value
            opt.text = varDisplay [i]; // set the text
            sel.appendChild(opt); // add it to the select
        }

}

for the first two part i already tested it and it is working but for the last parts i cant make it work

share|improve this question
up vote 1 down vote accepted

I think this part looks suspicious

<body id="body"  onclick="cmbRuleField(\'' + <?php echo json_encode($varArray);?> + '\');"    >

maybe

<body id="body"  onclick="cmbRuleField(<?php echo json_encode($varArray);?>)">

is more like it.

One more tip, you can see the output on the rendered page to determine what the written out code looks like. So if you see something like:

<body id="body"  onclick="cmbRuleField('['a', 'b']')">

you know there is a problem. You want a native Javascript array to be passed like this

<body id="body"  onclick="cmbRuleField(['a', 'b'])">

EDIT

After talking on chat it became clear the top portion of OP's code needed a tweak as well.

header("location: Rules.php?varFields=".http_build_query($varFields));
share|improve this answer
    
can you elaborate man? – user3287181 Feb 21 '14 at 18:59
1  
Just use the second line from my example bro. You are trying to put single quotes around the array so it's not getting passed to cmbRuleField as an array, you see what I mean? – quickshiftin Feb 21 '14 at 19:00
    
Also, take a look at the code in the browser, like I suggested in my update and you will see what I mean. You need to pass an array to cmbRuleField, not a string. – quickshiftin Feb 21 '14 at 19:01
1  
You're passing it a serialized php array instead of a json_encoded array! Take a closer look. You want to pass a Javascript array. – quickshiftin Feb 21 '14 at 20:31
1  
Click on the link for the chat. I'm in there now, ready to help out. – quickshiftin Feb 21 '14 at 20:42

The problem has to do with quotes not being terminated here:

...
<body id="body"  onclick="cmbRuleField(\'' + <?php echo json_encode($varArray);?> + '\');"    >
...

The JSON created using json_encode will have a lot of double quotes. Try this:

<script>
var array = <?php echo json_encode($varArray);?>;
</script>
<body id="body" onclick="cmbRuleField(array);">
share|improve this answer
    
then when it is already in the jS function i just have to use the array as is bro? – user3287181 Feb 21 '14 at 18:55
    
correct, remove the varDisplay variable – Steve Farthing Feb 21 '14 at 18:57
    
ill get back to you wait – user3287181 Feb 21 '14 at 19:00
    
unfortunately it is still not working another idea? – user3287181 Feb 21 '14 at 19:09
    
Any errors come up in the console? Can you post your code? – Steve Farthing Feb 21 '14 at 19:49

There is a much easier way. Encode the $varArray as direct HTML options before sending to the browser. For instance:

<select id="ruleField">
<?php for ($i = 0; $i < count($varArray); $i++) { ?>
        <option value="<?php= $varArray[$i].val ?>"><?php= $varArray[$i].name ?></option>
<?php } ?>       
</select>
share|improve this answer
2  
That looks painful! json_encode is probably the right direction. – quickshiftin Feb 21 '14 at 18:58
    
Or even better, make a dedicated php function to take any array and return a list of options. You will probably use that a lot. Then you can do thinks like `<select id="ruleField"><?php= optionsFor($varArray) ?></select> – Keith Feb 21 '14 at 19:00
    
@quickshiftin: painful? Sending an array as a JSON string to the browser and then using browser-specific code to build html elements on the fly is not painful? The user is already building the rest of the page (forms, select fields etc) in php. 3 lines of additional PHP vs. hours of debugging javascript sounds anything but painful. – Keith Feb 21 '14 at 19:03
2  
Yes painful. There's no need to build out a ton of extra markup when you can send an array directly as OP is trying to do. Hours of debugging, that's hilarious bro, keep the jokes coming! – quickshiftin Feb 21 '14 at 20:33

Might be because you are calling JSON.stringify on something that is already a string?

Also what is myCars?

..
for (var i in myCars)
..

Possibly rename it to varArray.

or rename varDisplay to varArray.

and lastly try calling JSON.parse instead:

function cmbRuleField(varArray)//ruleField 
{   
    var varDisplay = JSON.parse(varArray);

        var sel = document.getElementById("ruleField") // find the drop down

        for (var i in myCars) 
        { // loop through all elements

            var opt = document.createElement("option"); // Create the new element
            opt.value = varDisplay [i]; // set the value
            opt.text = varDisplay [i]; // set the text
            sel.appendChild(opt); // add it to the select
        }

}

If that didn't work post the html output here so peeps can create a fiddle :)

share|improve this answer
    
i already tried doing that one bro by the way I already change myCars to varDisplay, incase you will notice, any other idea bro? and i dont have any html error or stuffs it just does not show anything in my drop down list bro. – user3287181 Feb 21 '14 at 18:58

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.