1

I am trying to pass some value from a PHP array into JavaScript.

My php variable $contacts_info has the followiing values.

Array ( [0] => Array ( [contact_id] => 20 [type_id] => 2 [contact_type] => Representative Phone [contact_value] => 0987654321 )
        [1] => Array ( [contact_id] => 21 [type_id] => 4 [contact_type] => Shop Address [contact_value] => 0987654321 ) 
        [2] => Array ( [contact_id] => 22 [type_id] => 3 [contact_type] => Emergency Phone [contact_value] => 0987654321 ) 
        [3] => Array ( [contact_id] => 23 [type_id] => 2 [contact_type] => Representative Phone [contact_value] => 0987654321 ) 
        [4] => Array ( [contact_id] => 24 [type_id] => 3 [contact_type] => Emergency Phone [contact_value] => 0987654321 ) 
        [5] => Array ( [contact_id] => 25 [type_id] => 4 [contact_type] => Shop Address [contact_value] => 0987654321 ) ) 1

I am trying to assign a specific value in some Combobox with a for loop using JavaScript. But I am having a problem doing so.

Type: ParseError

Message: syntax error, unexpected '<', expecting ']'

My JavaScript code:

<script>
    var total_counter = "<?php echo $total_contact; ?>";
    var contact_info = "<?php json_encode($contacts_info); ?>";
    for (var i = 0; i <= total_counter - 1; i++) {
        let element = document.getElementById("contact_type_id_" + i);
        element.value = "<?php echo $contacts_info[<script>document.writeln(i);</script>]['type_id'] ?>";
    }
</script>

The problem is in this part <?php echo $contacts_info[<script>document.writeln(i);</script>]['type_id'] ?>. Can anyone please help me with it.

Thank you.

Mohsin
  • 465
  • 6
  • 15
  • `var contact_info = ;` <- you don't want the quotes (as you want an array and not a string, and even if were a string `json_encode` would add them anyway), and you want to echo the result. Alternatively: `var contact_info = = json_encode($contacts_info) ?>;` – Jeto Aug 24 '20 at 18:30
  • Php runs on server and Javascript runs in browser long after php has completed. You don't need php in the javscript loop since you are already passing the php array as json to variable above the loop – charlietfl Aug 24 '20 at 18:31
  • You also don't want quotes around `` but are also missing `echo` – charlietfl Aug 24 '20 at 18:33
  • This will help with json: https://stackoverflow.com/questions/63527595/custom-json-error-response-fails-when-no-error/63528831#63528831 – SJacks Aug 24 '20 at 18:34
  • I wonder about the error... `Message: syntax error, unexpected '<', expecting ']'` ... there is no `<` in the resulting code. At least there shouldn't be. – Flash Thunder Aug 24 '20 at 18:35

1 Answers1

3

Please do as follows,

  1. You need to remove double quotes that enclose the PHP script, as "" treats it a string.

  2. <?php echo json_encode($contacts_info); ?> was missing echo as JSON array wasn't getting echoed such that it can be assigned to the javascript variable contact_info

  3. The variable contact_info already have your JSON array, no need to again get it from PHP variable,

Eventually your code should look like,

 <script>
        var total_counter = <?php echo $total_contact; ?>;
        var contact_info = <?php echo json_encode($contacts_info); ?>;
        for (var i = 0; i <= total_counter - 1; i++) {
            let element = document.getElementById("contact_type_id_" + i);
            element.value = contact_info[i].type_id ;
        }
    </script>
Firdous bhat
  • 105
  • 7