0

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="{&quot;id&quot;:&quot;1&quot;,&quot;name&quot;:&quot;Arm&quot;,&quot;min&quot;:&quot;15000&quot;,&quot;max&quot;:&quot;25000&quot;}">Arm</option><option value="{&quot;id&quot;:&quot;3&quot;,&quot;name&quot;:&quot;Head&quot;,&quot;min&quot;:&quot;100000&quot;,&quot;max&quot;:&quot;150000&quot;}">Head</option><option value="{&quot;id&quot;:&quot;2&quot;,&quot;name&quot;:&quot;Leg&quot;,&quot;min&quot;:&quot;30000&quot;,&quot;max&quot;:&quot;45000&quot;}">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"}

1 Answer 1

1

The mistake is in jQuery.parseJSON(jQuery(injury));. You call jQuery() with the unparsed JSON. But jQuery expects a selector and the JSON is not a valid one.

Try: jQuery.parseJSON(injury);

Sign up to request clarification or add additional context in comments.

1 Comment

I guess I got carried away with my jQuery"ing". Thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.