Jscript & php in select drop downs

I having a problem with the  results I'm getting from the following jscipt and how it's inter acting with the php code  for the drop downs.

JSCRIPT:
<script type="text/javascript">
			$( document ).ready( function ()
			{
				$( '#company' ).on( 'change', function () {  //on change event of the select for Companies
					var company_id = $( '#company option:selected' ).val();
					var count = 0;
					$.ajax(
					{
						type: 'post',
						url: 'persons.php',
						data: { TID: company_id },
						dataType: 'json',
						success: function ( resultArray )
						{
							$('#agent_1').children().remove();
							$('#agent_2').children().remove();
							$('#agent_3').children().remove();
							$( resultArray ).each( function ( k, v )
							{
								$( v ).each( function ( a, b )
								{
									var select_text = "";
									var work = b['LNAME'] + ", " + b['FNAME'];
									if (b['PNID'] = "0000")
									{
										select_text = " selected";
										work = b['LNAME'];
									}
									$( '#agent_1' ).append( "<option value='" + b['PNID'] + "'" + select_text + ">" + work + "</option>" );
									$( '#agent_2' ).append( "<option value='" + b['PNID'] + "'" + select_text + ">" + work + "</option>" );
									$( '#agent_3' ).append( "<option value='" + b['PNID'] + "'" + select_text + ">" + work + "</option>" );
									count++;
								} );
							} );
							$('#queryResult').text(count);
						},
						error: function ( jqXHR, textStatus, errorThrown )
						{
							console.log( textStatus + " " + errorThrown );
						}
					} );
				} );
			} );
		</script>

Open in new window

PHP:

					echo '<select id="agent_1" name = "Agent_1" width="200" style="width: 200px">';
					$SqlString = "select * from pers where TID = '$ROW2[TID]'";
					$persons = mysql_query($SqlString,$conn) or die( mysql_error() );
					while ($ROW_Persons = mysql_fetch_array($persons, MYSQL_BOTH))
					{
						echo "<option value = \"";
						echo $ROW_Persons['PNID'];
						echo "\"";
						if ($ROW_Persons['PNID'] == $ROW['PNID'])
					 	{
							echo " selected";
						}
						echo ">";
						$work = $ROW_Persons['LNAME'];
						if ($ROW_Persons['PNID'] > "0000")
						{
							$work = $work.", ".$ROW_Persons['FNAME'];							
						}
						echo $work;
						echo "</option>";
					}
					echo "</select>"

Open in new window


Problems:
1: The Jscript works as expected excepted.  When the company is changed all agents are deleted and  the "Unknow" agent (PID = 0000) is hi-lighted in the drop down.  But the display is only the last name for all id's.

2: After changing the company and selecting a different agent, the display in the drop down is changed, however, the post results from the form still reports "0000".  This doesn't occur if the company has not been changed.
breeze351Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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.

Chris StanyonWebDevCommented:
Your jQuery script is expecing JSON data back from your PHP, but your PHP is generating HTML.
0
Chris StanyonWebDevCommented:
You're also trying to use a value of $ROW2[TID] in your query - that value is passed to your PHP script from your jQuery as $_POST['TID']
0
breeze351Author Commented:
Ok, maybe I'm explaining this wrong.

The script works correctly.  It changes the company in the "company" drop down and populates the children drop downs with the correct agents.  The query that you referred to  only gets executed when the page loads.  

After the change of the company and the selection of an agent, I have an update button which goes to "Update_Space.php".   This is where the post data shows the new company id but only shows "0000" for the agent.  I have looked at the code in the browser after the change of company and the data for the agent drop down is not correct, it still shows the agents for the original company.

Let's tackle this first and then go back to the display problem in the script.
0
Chris StanyonWebDevCommented:
OK. One thing that I see obviously wrong is that you use an assignment (=), rather than a comparison (==) in your if statement:

if (b['PNID'] = "0000")

should be:

if (b['PNID'] == "0000")

If that's not it then we'll need to see the html and the data the PHP script is sending back to your jQuery. It appears that the PHP script you posted has nothing to do with your question.
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
breeze351Author Commented:
That was it!!!!!  Sometimes all you need is a 2nd set of eyes ;)
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
JScript

From novice to tech pro — start learning today.