cjkeane Posted April 4, 2011 Share Posted April 4, 2011 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> Quote Link to comment https://forums.phpfreaks.com/topic/232695-passing-value-from-dropdown-box-to-textbox/ Share on other sites More sharing options...
dcro2 Posted April 4, 2011 Share Posted April 4, 2011 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(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/232695-passing-value-from-dropdown-box-to-textbox/#findComment-1196844 Share on other sites More sharing options...
cjkeane Posted April 4, 2011 Author Share Posted April 4, 2011 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> Quote Link to comment https://forums.phpfreaks.com/topic/232695-passing-value-from-dropdown-box-to-textbox/#findComment-1196851 Share on other sites More sharing options...
dcro2 Posted April 4, 2011 Share Posted April 4, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/232695-passing-value-from-dropdown-box-to-textbox/#findComment-1196862 Share on other sites More sharing options...
cjkeane Posted April 4, 2011 Author Share Posted April 4, 2011 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> Quote Link to comment https://forums.phpfreaks.com/topic/232695-passing-value-from-dropdown-box-to-textbox/#findComment-1196870 Share on other sites More sharing options...
cjkeane Posted April 4, 2011 Author Share Posted April 4, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/232695-passing-value-from-dropdown-box-to-textbox/#findComment-1196881 Share on other sites More sharing options...
dcro2 Posted April 4, 2011 Share Posted April 4, 2011 There shouldn't be any problem... it works fine for me. Quote Link to comment https://forums.phpfreaks.com/topic/232695-passing-value-from-dropdown-box-to-textbox/#findComment-1196895 Share on other sites More sharing options...
cjkeane Posted April 4, 2011 Author Share Posted April 4, 2011 i figured it out. thx for mentioning firebug. i haven't used it for quite a while, but it helped me identify why it didnt work. cheers! There shouldn't be any problem... it works fine for me. Quote Link to comment https://forums.phpfreaks.com/topic/232695-passing-value-from-dropdown-box-to-textbox/#findComment-1196911 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.