-2

I tested the following:

<? echo json_encode($array) ?>    outputs: ["PG","Kevin Sad","8000","12"]

When I place it inside a form option value to be chosen by my script function:

<option value=<? echo json_encode($array) ?> > option1 </option>      

When I click SUBMIT button and the following function fires from the script:

function submit_button(){
    var data=  player1.options[player1.selectedIndex].value;
    document.getElementById('print_result').innerHTML = data; 
}

only the following is outputted inside the div id="print_result" :

["PG","Kevin

Something happens with the space character " " that I cannot figure out..

I expected whole array to show up specifically including missing portion:

 Sad","8000","12"]   
Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
  • 3
    This has nothing to do with Java nor JavaScript (yes, [these are not the same](https://stackoverflow.com/a/245069)) so I removed the tags. Anyway from what I remember values of attributes should be surrounded by quotes like `value="..."` or `value='...'` so try maybe with ` – Pshemo Jun 04 '17 at 17:01
  • wow single quotes was the culprit!! thanks so much! sorry for wrong tags – wooftime Jun 04 '17 at 17:07

1 Answers1

0

Either you have to escape the double quotes and/or you use single quotes to contain the JSON.

See example and how it effects the outcome.

let options = document.querySelectorAll('option');
console.log(options[0].value, options[1].value);
document.getElementById('out').innerHTML = options[1].value;
<option value=["PG","Kevin Sad","8000","12"]> option1 </option>
<option value='["PG","Kevin Sad","8000","12"]'> option2 </option>
<div id="out"></div>
Thomas Wikman
  • 696
  • 4
  • 11