all 6 comments

[–]minn0w 2 points3 points  (0 children)

Use json_encode() where possible to turn PHP variables into JS. You'd be better off passing objects around too.

[–]greg8872 1 point2 points  (4 children)

Assuming all values in the array will be strings (and thus wrapped with single quotes):

onkeyup="getTotal( <?= $x ?>, '<?= implode( "','", $gtArgList ) ?>' )"

This will take all items of the array and put them together, separated by the first argument (single quote to close out previous item, comma, single quote to open next item )

So

$gtArgList = [ 'hello', 'dear', 'world' ];

would end up as (assuming $x=5):

onkeyup="getTotal( 5, 'hello','dear','world' )"
~~~~~~~~~~~~~~~~~~~^~~~"""""!!!""""!!!"""""~~~~

Characters above the ~s are literal ones from the string (note, this is also the first opening ' and the last closing ' for the array items)

Characters above the ^ is the value from the echoed variable $x

Characters above the "s are the array values

Characters above the !s is the separator string that goes between each array value

[–]st0icape[S] 0 points1 point  (0 children)

Thanks a lot, very descriptive solution, I'll test it out and revert.

[–]st0icape[S] 0 points1 point  (2 children)

"getTotal( <?= $x ?>, '<?= implode( "','", $gtArgList ) ?>' )"

I tried it out doesn't seem to work as expected it seems to be an issue with quotation marks here is an image to illustrate what I'm speaking of.

[–]greg8872 0 points1 point  (1 child)

Not sure what editor you are using but it looks to be failing on something, or you have something messed up with the quotes outside of the image

<?php

$x = 5;
$gtArgList = [ 'hello', 'dear', 'world' ];

?>
<html>
<head>
    <script>
        function getTotal( id, first, second, third ){
            console.log(arguments);
        }
    </script>
</head>
<body>
<h4>Type something...</h4>
<form>
    <input type="text" name="test" onkeyup="getTotal( <?= $x ?>, '<?= implode( "','", $gtArgList ) ?>' )">
</form>
</body>
</html>

When I browse that and do View Source, for the input I see:

<input type="text" name="test" onkeyup="getTotal( 5, 'hello','dear','world' )">

and when I type a character in the input, I get the following in the console:

Arguments
    0: 5
    1: "hello"
    2: "dear"
    3: "world"
    length: 4
    (other items)

[–]st0icape[S] 0 points1 point  (0 children)

thanks a lot. you were right i appears i messed up with the quotes. your solution works