Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If you have HTML like this:</p> <pre><code>&lt;select id='clients'&gt;...&lt;/select&gt; &lt;select id='projects'&gt;...&lt;/select&gt; </code></pre> <p>You can have jQuery code like this:</p> <pre><code>$(document).ready(function() { var $clients = $('#clients'); $clients.change(function() { var client_id = $clients.val(); $.getJSON("getProjects.php", {id: client_id}, function(projects) { $projects = $('#projects'); $projects.empty(); $.each(projects, function() { var option = $('&lt;option/&gt;').attr('value', this.id).text(this.name); $projects.append(option); }); }); }); }); </code></pre> <p>And then have getProjects.php return something like:</p> <pre><code>$sth = mysql_query( sprintf( "SELECT * FROM projects WHERE client_id = %s", mysql_real_escape_string($_GET['id']) ) ); $projects = array(); while($r = mysql_fetch_assoc($sth)) { $projects[] = array('id' =&gt; $r['id'], 'name' =&gt; $r['name']); } print json_encode($projects); exit; </code></pre> <p>I haven't tested all of this, but it is more or less what you want, I think.</p> <p><strong>edit</strong> - Tested and works, obviously it might not be exactly what you need but it gives you an idea of how to go about it. Hope it helps.</p> <p><strong>edit 2</strong> - The problem with your code is here:</p> <pre><code>$.getJSON("../inc/get-projects.php", {id: client_id}, function(projects){ projects = $('#p_id'); projects.empty(); $.each(projects, function() { var option = $('&lt;option/&gt;').attr('value', this.id).text(this.name); projects.append(option); }); }); </code></pre> <p>You are overwriting the <code>projects</code> passed into the function with the <code>projects</code> in the 2nd line. I used a <code>$</code> in my code to differentiate the two, which was admittedly perhaps not the best of ways. To fix, change the code to this:</p> <pre><code>$.getJSON("../inc/get-projects.php", {id: client_id}, function(data){ projects = $('#p_id'); projects.empty(); $.each(data, function() { var option = $('&lt;option/&gt;').attr('value', this.id).text(this.name); projects.append(option); }); }); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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