fork download
  1. <select id="dropdown">
  2. <?php
  3. // this PHP is essentially the same as in the above example
  4. $users = [
  5. [
  6. "forename"=>"a_name",
  7. "dob"=>"never",
  8. "height"=>"too_tall",
  9. "weight"=>"too_heavy",
  10. "nationality"=>"martian"
  11. ],
  12. [
  13. "forename"=>"second_name",
  14. "dob"=>"always",
  15. "height"=>"too_short",
  16. "weight"=>"too_light",
  17. "nationality"=>"moon"
  18. ],
  19. [
  20. "forename"=>"third_name",
  21. "dob"=>"forever",
  22. "height"=>"300ft",
  23. "weight"=>"2000lb",
  24. "nationality"=>"Earth"
  25. ]
  26. ];
  27. $userDataArray = []; // multidimensional array
  28. $user_id = 0;
  29. foreach($users as $user) {
  30. $data = $user["forename"];
  31. echo "<option value='$user_id'>$data</option>";
  32. $user_id++;
  33. $userDataArray[] = [ "forename"=>$user["forename"], "dob"=>$user["dob"], "height"=>$user["height"], "weight"=>$user["weight"], "nationality"=>$user["nationality"] ];
  34. // index 0 = forename, 1 = DOB, 2 = height, 3 = weight, 4 = nationality
  35. }
  36. ?>
  37. </select><br />
  38. <span id="display"></span>
  39. <script src="https://a...content-available-to-author-only...s.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script><!-- load jQuery library -->
  40. <script>
  41. var users = [
  42. <?php // translate PHP array to JS one
  43. foreach($userDataArray as $user) {
  44. echo "{ forename: '" . $user["forename"] . "', dob: '" . $user["dob"] . "', weight: '" . $user["weight"] . "', height: '" . $user["height"] . "', nationality: '" . $user["nationality"] . "'},";
  45. }
  46. ?>
  47. ];
  48. $("#dropdown").change(function() {
  49. var i = $("#dropdown").val();
  50. $("#display").html("Name: " + users[i].forename + "<br />DOB: " + users[i].dob + "<br />Weight: " + users[i].weight + "<br />Height: " + users[i].height + "<br />Nationality: " + users[i].nationality);
  51. });
  52. </script>
Success #stdin #stdout 0.02s 52432KB
stdin
Standard input is empty
stdout
<select id="dropdown">
<option value='0'>a_name</option><option value='1'>second_name</option><option value='2'>third_name</option></select><br />
<span id="display"></span>
<script src="https://a...content-available-to-author-only...s.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script><!-- load jQuery library -->
<script>
    var users = [
        { forename: 'a_name', dob: 'never', weight: 'too_heavy', height: 'too_tall', nationality: 'martian'},{ forename: 'second_name', dob: 'always', weight: 'too_light', height: 'too_short', nationality: 'moon'},{ forename: 'third_name', dob: 'forever', weight: '2000lb', height: '300ft', nationality: 'Earth'},    ];
    $("#dropdown").change(function() {
		var i = $("#dropdown").val();
        $("#display").html("Name: " + users[i].forename + "<br />DOB: " + users[i].dob + "<br />Weight: " + users[i].weight + "<br />Height: " + users[i].height + "<br />Nationality: " + users[i].nationality);
    });
</script>