-1

I've to do a variable array with variable name based on ID set by PHP echo.

    <script>
  var dat<?php echo $id; ?>;
  dat<?php echo $id; ?> = ["ecc..","ecc.."];
  </script>

jQuery("#name").html(dat[0]+id); // doesn't work

id is already set by a function, the question is: can i write the VAR correctly including also the PHP ID? Thanks.

Thanks

User19384
  • 37
  • 6

1 Answers1

1

Try:

<script>
var dat<?php echo $id; ?>;
dat<?php echo $id; ?> = ["ecc..","ecc.."];

console.log(dat<?php echo $id; ?>[1]);
jQuery("#name").html(dat<?php echo $id; ?>[0]);
</script>

The reason it wasnt working is because dat is dat# where # is an id.

You must access an array element on dat#, not dat[pos]#.

Haring10
  • 1,517
  • 1
  • 19
  • 37