Feeding a PHP array into a Javascript Array/Drop Down -
i'm trying create webpage has drop downs populated values supplied oracle database query. code i'm using has worked me before when querying sql server, modified oracle. outside of skillset - i'm more of sys admin developer: it's 1 of cases there no 1 else it. php code queries oracle (the var_dump erify array populated values form oracle):
**<?php // create connection oracle $conn = oci_pconnect("user", "password", "//oracleserver/prod"); $query = 'select distinct terms_cd terms order terms_cd'; $stid = oci_parse($conn, $query); $r = oci_execute($stid); $j = 0; while ($row = oci_fetch_array($stid, oci_return_nulls+oci_assoc)) { foreach ($row $item) { $termscode[] = array("id" => $j, "val" => $item); $j++; } } //verify array //var_dump($termscode); $jsontermscode = json_encode($termscode); ?>**
this javascript:
<script type='text/javascript'> <?php> echo "var termscode = $jsontermscode; \n"; ?> function loadtermscode() { var select1 = document.getelementbyid("termscodeselect"); for(var = 0; < termscode.length; i++) { var terms = document.createelement("option"); //terms.text = termscode[i]; //select1.add(terms, null); select1.options[i] = new option(termscode[i].val, termscode[i].id); echo select1.options[i] } } </script>
and html:
<body onload="loadfunc(), loadtermscode()"> <form id="f" method="post" name="vendorchange" > <!--div 1--> <div id="container"> <!--div 2--> <div id="divtabcontainer"> <fieldset> <table> <td class="dept"> terms code: </td> </tr> <tr> <td> <select id="termscodeselect" name="termcode" > </select> </td> </tr> </table> </fieldset> </div> </form> </body>
the drop down not populate - i've been beating head on , can't figure out why. insight?
the "echo" have inside javascript must causing error. "echo" in javascript need "document.write()".
besides that, code have commented out seems correct based on http://www.w3schools.com/jsref/met_select_add.asp except index being null. ignore index if dont want assign one.
i referring code:
var terms = document.createelement("option"); terms.text = termscode[i]; select1.add(terms);
Comments
Post a Comment