1

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

4 Answers 4

1

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));
Sign up to request clarification or add additional context in comments.

11 Comments

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?
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.
okay i will try it and get back to you thanks bro just a moment
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.
Click on the link for the chat. I'm in there now, ready to help out.
|
1

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);">

4 Comments

then when it is already in the jS function i just have to use the array as is bro?
ill get back to you wait
Any errors come up in the console? Can you post your code?
nothing show ups bro, the drop down just does not show anything
1

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>

4 Comments

That looks painful! json_encode is probably the right direction.
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>
@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.
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!
0

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 :)

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.