Note that there are some explanatory texts on larger screens.

plurals
  1. POPopulate Select list Menu though Ajax Jquery
    primarykey
    data
    text
    <p>Before anybody says this is a duplicate of this and that question, let me assure you I have tried the solutions there and I have failed. I am using a solution offered in <a href="http://www.codingcereal.com/2009/09/autopopulate-select-dropdown-box-using-jquery/" rel="nofollow noreferrer">this</a> website to come up with my solution and I believe I am 90% done except for one error. I want to display a list of all codes that have a certain common ID associated with them. </p> <p>Here is my PHP code that I am using to get a list of codes</p> <pre><code>&lt;?php $budgetcode=$_POST['budgetcode']; //$budgetcode=2102; $selectcodes="SELECT * FROM tblbudget_codes WHERE T1 = $budgetcode"; $query=$connection-&gt;query($selectcodes); $count=$query-&gt;num_rows; if($count &lt; 1) { die('0'); } else{ while($row=$query-&gt;fetch_array()){ $T1=($row['T1']); $T2=($row['T2']); $T3=($row['T3']); $T4=($row['T4']); $optionValue = $T1."-".$T2."-".$T3."-".$T4; echo json_encode("&lt;option&gt;$optionValue&lt;/option"); // echo json_encode('1'); } } ?&gt; </code></pre> <p>Here is the ajax call i am using to fetch the codes</p> <pre><code>$.post("Functions/getbudgetcodes.php",{budgetcode:budgetid},function(data){ if(data!='0') { $("#budgetcode").html(data).show(); $("#result").html(''); } else{ $("#result").html('&lt;em&gt;No codes found. Contact Administrator&lt;/em&gt;'); } },'json') //alert(budgetid); }) </code></pre> <p>The problem here is that jquery does not understand the data it is receiving if it is not numeric. E.g if I comment out the json_encode('1') and put random html code instead of data in my success part, I get results displayed in my browser. Can anybody tell me why jquery is only recognizing numeric values that are being echoed from PHP and not varchar values. Using jquery 1.4.2. Any help appreciated. </p> <p><strong>EDIT</strong><br> I have managed upto some point and now i am stuck. I have used John's Answer and here is my jquery code. i just need to split the array and append each element to a variable one at a time like <a href="https://stackoverflow.com/questions/1502649/jquery-getjson-populate-select-menu-question">here</a></p> <p>here is the code. Somebody please tell how I split (data). i can alert it but it is comma seperated. Just need to get the individual items append them to variable html and then display it.</p> <pre><code>$.post("Functions/getbudgetcodes.php",{budgetcode:budgetid},function(data){ if(!$.isEmptyObject(data)) { //alert (data); // alert(split (data)) var html = ''; var len = data.length; for (var i = 0; i&lt; len; i++) { html += '&lt;option&gt;' +data+ '&lt;/option&gt;'; } $("#budgetcode").html(html).show(); $("#result").html(''); } else{ $("#result").html('&lt;em&gt;No codes found. Contact Administrator&lt;/em&gt;'); } },'json') </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload