Jump to content

Passing value from dropdown box to textbox.


cjkeane

Recommended Posts

hi everyone.

 

I have two dropdown lists 1 with company name and the other with company branch. when the company name is selected, the value goes into a textbox. when the dropdown box value changes it changes the second dropdown box to find related company branches. i'm having a problem passing the value selected from the second dropdown box to the company branch textbox. i'm hoping someone can help me figure this out.

 

<script language="javascript" type="text/javascript">
function CompanyNameCBtoTB(){
	document.getElementById("CompanyName").value=document.getElementById("drop_1").value
}
</script>
<script language="javascript" type="text/javascript">
function CompanyBranchCBtoTB(){
	document.getElementById("CompanyBranch").value=document.getElementById("tier_two").value
}
</script>
       
<select name="drop_1" id="drop_1" onchange="CompanyNameCBtoTB()">
          <option value="" selected="selected" disabled="disabled">< select principal ></option>
      
          <?php
		//**************************************
		//     Page load dropdown results     //
		//**************************************
		function getTierOne()
		{
			$result = mysql_query("SELECT DISTINCT CompanyName FROM principal") 
			or die(mysql_error());
			  while($tier = mysql_fetch_array( $result )) {
				   echo '<option value="'.$tier['CompanyName'].'">'.$tier['CompanyName'].'</option>';
				}
		}

		//**************************************
		//     First selection results     //
		//**************************************
		if($_GET['func'] == "drop_1" && isset($_GET['func'])) { 
		   drop_1($_GET['drop_var']); 
		}

		function drop_1($drop_var)
		{  
			include_once('db.php');
			$result = mysql_query("SELECT Distinct BranchName FROM principal WHERE CompanyName='$drop_var'") 
			or die(mysql_error());
			echo '<select name="tier_two" id="tier_two" onchange=\"CompanyBranchCBtoTB()\">
				  <option value=" " disabled="disabled" selected="selected">Choose one</option>';
				   while($drop_2 = mysql_fetch_array( $result )) {
					  echo '<option value="'.$drop_2['BranchName'].'">'.$drop_2['BranchName'].'</option>';
					}
			echo '</select> ';
			}
		?>
          
          <?php getTierOne(); ?>
                    </select> 
        
        <span id="wait_1" style="display: none;">
          <img alt="Please Wait" src="ajax-loader.gif"/>
          </span>
        <span id="result_1" style="display: none;"></span> 

 

Link to comment
Share on other sites

Err.. I think you need to end the first <select> before starting another one. So call getTierOne() AFTER </select>. Also, you should probably exit the script at the end of drop_1() so that getTierOne() doesn't get called on an AJAX request.

 

</select>
<?php getTierOne(); ?>

Link to comment
Share on other sites

hmm. thanks for the reply.  if it switch the end select around:

 

<?php getTierOne(); ?>

</select>

 

with your suggestion of:

 

</select>

<?php getTierOne(); ?>

 

then that affects the first dropdown box - no results are found in the drop_1 dropdown box.

Any other suggestions?

 

I did change the code around a bit to following. both dropdown boxes work, but the value of tier_two still doesn't pass to companybranch:

 

  <select name="drop_1" id="drop_1" onchange="CompanyNameCBtoTB()">

         

          <option value="" selected="selected" disabled="disabled">< select principal ></option>

          <?php getTierOne(); ?>

        </select>

       

        <span id="wait_1" style="display: none;">

          <img alt="Please Wait" src="ajax-loader.gif"/>

          </span>

        <span id="result_1" style="display: none;"></span>

 

 

Link to comment
Share on other sites

Wait.. you're right. That makes no sense haha. Can you post the code for the textboxes at least?

 

It's kind of confusing how you posted the code because a lot of it is missing, like where the second dropbox is put on the page (with AJAX I assume). That big block of PHP should really be at the top so an AJAX call won't output all the other stuff on the page.

 

EDIT: Ok, good changes. If your second dropdown gets generated by AJAX, have you checked it's proper by using something like Firebug or Inspect Element in Chrome/Safari?

Link to comment
Share on other sites

OK. Here's the entire code. i'm really not sure why the value of tier_two isn't passed to the 'CompanyBranch' textbox. I'm using the same javascript code that i used to pass the value of drop_1 to 'CompanyName'. hopefully this can be resolved easily. thanks for the assistance.

 

<table width="100%" border="0" cellspacing="1" cellpadding="1">
   <tr>
      <td colspan="2"> <span class="text-align">Principal/Branch *</span>
          <script language="javascript" type="text/javascript">
	function CompanyNameCBtoTB(){
		document.getElementById("CompanyName").value=document.getElementById("drop_1").value
	}
</script>
         <script language="javascript" type="text/javascript">
	function CompanyBranchCBtoTB(){
	      document.getElementById("CompanyBranch").value=document.getElementById("tier_two").value
	}
</script>
       
        <?php
	//**************************************
	//     Page load dropdown results     //
	//**************************************
	function getTierOne(){
	$result = mysql_query("SELECT DISTINCT CompanyName FROM principal") 
		or die(mysql_error());
		  while($tier = mysql_fetch_array( $result )) {
		       echo '<option value="'.$tier['CompanyName'].'">'.$tier['CompanyName'].'</option>';
		  }
	}

	//**************************************
	//     First selection results     //
	//**************************************
	if($_GET['func'] == "drop_1" && isset($_GET['func'])) { 
	   drop_1($_GET['drop_var']); 
	}

	function drop_1($drop_var){  
	include_once('db.php');
	$result = mysql_query("SELECT Distinct BranchName FROM principal WHERE CompanyName='$drop_var'") or die(mysql_error());
	echo '<select name="tier_two" id="tier_two" onchange="CompanyBranchCBtoTB()">
		  <option value=" " disabled="disabled" selected="selected">Choose one</option>';
		   while($drop_2 = mysql_fetch_array( $result )) {
			  echo '<option value="'.$drop_2['BranchName'].'">'.$drop_2['BranchName'].'</option>';
			}
		echo '</select> ';
		}
		?>
          
        <select name="drop_1" id="drop_1" onchange="CompanyNameCBtoTB()">
             <option value="" selected="selected" disabled="disabled">< select principal ></option>
         <?php getTierOne(); ?>
         </select>
        
        <span id="wait_1" style="display: none;">
          <img alt="Please Wait" src="ajax-loader.gif"/>
          </span>
        <span id="result_1" style="display: none;"></span> 
        
       </td>
      </tr>
    <tr>
  <td colspan="2"><span class="text-align">><span class="required-fields"></span></span>
  <input name="CompanyName" type="text" id="CompanyName" value="<?php echo $CompanyName ?>" size="29"  />  
<input name="CompanyBranch" type="text" id="CompanyBranch" value="<?php echo $CompanyBranch ?>" size="47"  /></td>
   </tr>
</table>

Link to comment
Share on other sites

I just checked the page using firebug and i can see that there is an issue.

for the first dropdown box I see: <select id="drop_1" onchange="CompanyNameCBtoTB()" name="drop_1">

but for the socond dropdown box, i only see: <select id=tier_two" name="tier_two">

 

so its not seeing the onchange. it should appear as:

<select id=tier_two" onchange=CompanyBranchCBtoTB()" name="tier_two">

 

in my coding i do have:

echo '<select name="tier_two" onchange="CompanyBranchCBtoTB()" id="tier_two">

<option value=" " disabled="disabled" selected="selected">Choose one</option>';

 

is there a problem having an onchange inside an echo?

 

Wait.. you're right. That makes no sense haha. Can you post the code for the textboxes at least?

 

It's kind of confusing how you posted the code because a lot of it is missing, like where the second dropbox is put on the page (with AJAX I assume). That big block of PHP should really be at the top so an AJAX call won't output all the other stuff on the page.

 

EDIT: Ok, good changes. If your second dropdown gets generated by AJAX, have you checked it's proper by using something like Firebug or Inspect Element in Chrome/Safari?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.