I have a PHP function that returns database values to populate a select form field. I am trying to populate the value of the option elements with all relevant data from the query (id, name, min, max) from the database so I don't have to send a AJAX request to get the rest, so I decided to json_encode the php array before populating select field.
Relevant PHP:
$items[] = array( "text" => $injury['name'], "value" => json_encode($injury) );
HTML Output from PHP:
<select name="input_2" id="input_2_2" class="medium gfield_select" tabindex="4"><option value=" ">Select an Injury</option><option value="{"id":"1","name":"Arm","min":"15000","max":"25000"}">Arm</option><option value="{"id":"3","name":"Head","min":"100000","max":"150000"}">Head</option><option value="{"id":"2","name":"Leg","min":"30000","max":"45000"}">Leg</option></select>
In the javascript side I am using jQuery to get the value of the option as below:
`jQuery(injuryClass).on("change", function () {
var injurySelect = jQuery(this);
injury = injurySelect.val();
var results = jQuery.parseJSON(jQuery(injury));
console.log(results)
I get a console error:
uncaught error: syntax error, unrecognized expression: {"id":"1","name":"Arm","min":"15000","max":"25000"}