Jump to content

PHP dropdown menu to populate text boxes based on choice made.


atl_andy

Recommended Posts

I have a form with dropdowns that are populated from a SQL database.  What I need is to have two text boxes to populate based on the choice from one of the dropdowns.  How is this best accomplished with JS?

 

The sql query for the dropdown would be:

 
$queryPN = "SELECT partnumber FROM partnumber ORDER BY partnumber";

 

The sql table for partnumber also includes manufacturer and description.  When the partnumber is selected, I need the manufacturer and description associated with it to populate a text box on the form.

 

$queryMFR = "SELECT manufacturer, description FROM partnumber"; //These results need to go into a text box

Link to comment
Share on other sites

Revision of the question....

 

I did some research and added the reload(this.form) to the part number dropdown menu.  Will the js function still work if it is inside a php function?  If not, how would I get it to work in there?  It seems like all I need is to have the from reload on the change in the part number menu and that would trigger the manufacturer menu to populate.  Also, do I need to put any js in the MFR dropdown, or will it work automatically once the reload(this.form) works?

 

// PART NUMBER DROPDOWN

function create_dropdownPN($identifierPN,$pairsPN,$multiplePN=""){
   $dropdownPN = "<select onchange=\"reload(this.form)\" name=\"$identifierPN\"> ";
   $dropdownPN .= "<option value ='$namePN'></option>";
   foreach($pairsPN as $valuePN => $namePN){
      $dropdownPN .= "<option value='$namePN'>$namePN</option>";
   }
   $dropdownPN .= "</select><br><br>";
   return $dropdownPN;
   }

   //Connect to the db server and select a database
   $db_handlePN = pg_connect('dbname=secret user=secret password=secret');

   // Retrieve the manufacturer table data
   $queryPN = "SELECT DISTINCT record_id, partnumber FROM partnumber ORDER BY partnumber";
   $resultPN = pg_query($db_handlePN, $queryPN);

   // Create an associative array based on the table data
   while($rowPN = pg_fetch_array($resultPN))
   {
      $valuePN = $rowPN['record_id'];
      $namePN = $rowPN['partnumber'];
      $pairsPN[$valuePN] = $namePN;
   }
?>
   <p>Part Number: <? echo create_dropdownPN("partnumber",$pairsPN,"");?></p>
<?php
   pg_close($db_handlePN);

 

 

// MFR DROPDOWN

function create_dropdownMFR($identifierMFR,$pairsMFR,$multipleMFR=""){
   $dropdownMFR = "<select name=\"$identifierMFR\"> ";
   $dropdownMFR .= "<option value ='$nameMFR'></option>";
   foreach($pairsMFR as $valueMFR => $nameMFR){
      $dropdownMFR .= "<option value='$nameMFR'>$nameMFR</option>";
   }
   $dropdownMFR .= "</select><br><br>";
   return $dropdownMFR;
   }

   //Connect to the db server and select a database
   $db_handleMFR = pg_connect('dbname=secret user=secret password=secret');

   // Retrieve the manufacturer table data
   $queryMFR = "SELECT record_id, manufacturer FROM partnumber WHERE record_id LIKE '$pairsPN' ";
   $resultMFR = pg_query($db_handleMFR, $queryMFR);

   // Create an associative array based on the table data
   while($rowMFR = pg_fetch_array($resultMFR))
   {
      $valueMFR = $rowMFR['record_id'];
      $nameMFR = $rowMFR['manufacturer'];
      $pairsMFR[$valueMFR] = $nameMFR;
   }

   echo "Manufacturer: <br />";
   echo create_dropdownMFR("manufacturer",$pairsMFR,"");

   pg_close($db_handleMFR);

 

// JS function.  Need some help in getting this correct.

<script type="text/javascript">
    function reload(form) {
      var val=form.partnumber.options[form.partnumber.options.selectedIndex].value;
      self.location="pc.php" + val;

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.