How to append jQuery AJAX results in the form of <options> for my form?

Experts,

I am attempting to build my form dynamically based on what a user selects.

How can I append the results of my query as <option></option> tags to my second select?

My query is executing perfectly and returns a result but, I am unsure about the <option> portion of this task.

<script type="text/javascript">
$(document).ready(function() {
	$("#JobSite").change(function() {
		// SHOW LOADING IMAGE
		$('.loading_image_sm').show();
		$("#JobDepartment").load("./scripts/test.php?ajax_post=get_department", { value: $(this).val() }, 
			function() {
				//HIDE LOADING IMAGE WHEN QUERY COMPLETED SUCCESSFULLY
				$('.loading_image_sm').hide();			
				var items = [];
				$.each(data, function( key, val ) {
					items.push( "<option value='" + val + "'>" + val + "</option>" );
				});
				$( "#JobDepartments").append(items);
			}
		);
	});
});
</script>

Open in new window


<table cellpadding="2" cellspacing="2" border="0" width="100%">
    <tr>
        <td><b>Site Name: </b></td>
        <td>
            <select id="JobSite">
            <?php
                while($distinct_site = mysql_fetch_array($get_site_query)){
                    echo'<option value="'.$distinct_site['JobSite'].'">'.$distinct_site['JobSite'].'</option>';
                }
            ?>
            </select>
        </td>
    </tr>                
    <tr>
        <td><b>Department Name: </b></td>
        <td>
            <select id="JobDepartment">
                <?php echo $job_department; ?>
            </select>
            <img src="./images/ajax-loader_sm.gif" class="loading_image_sm" />
        </td>
    </tr>
</table>

Open in new window


test.php
		$job_department_sql = "SELECT DISTINCT JobDepartment FROM jobs WHERE JobSite='".$_POST['value']."' ORDER by JobDepartment ASC";
		$job_department_query = mysql_query($job_department_sql) or die ("Could not run query: " . $job_department_sql . "<br />\n" . mysql_error () );
		while($job_department = mysql_fetch_array($job_department_query)){			
			$departments[] = array("JobDepartment"=>$job_department['JobDepartment']);
		}
	//SEND AUTO-SUGGESTION BOX DATA IN A JSON FORMAT
	echo json_encode($departments);

Open in new window

evibesmusicAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

RobOwner (Aidellio)Commented:
Is there a reason you're sending json format?

If your test.php just returned the actual html like you do for your first select, it's as easy as leaving the .load()

e.g.

		$("#JobDepartment").load("./scripts/test.php?ajax_post=get_department", { value: $(this).val() }, 
			function() {
				//HIDE LOADING IMAGE WHEN QUERY COMPLETED SUCCESSFULLY
				$('.loading_image_sm').hide();			
			}
		);

Open in new window


test.php
		$job_department_sql = "SELECT DISTINCT JobDepartment FROM jobs WHERE JobSite='".$_POST['value']."' ORDER by JobDepartment ASC";
		$job_department_query = mysql_query($job_department_sql) or die ("Could not run query: " . $job_department_sql . "<br />\n" . mysql_error () );
		while($job_department = mysql_fetch_array($job_department_query)){			
                        echo'<option value="'.$job_department['jobDepartment'].'">'.$job_department['jobDepartment'].'</option>';
                
		}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
evibesmusicAuthor Commented:
@Rob (tagit):

Thank you. Sometimes I make things much more difficult than needed.

Simply returning the results of the PHP while loop worked perfectly.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
jQuery

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.