Hi guys! i have a dropdown list that is filled with data that is pulled from my database. Im stuck with populating the textbox based on the value thats selected from the dropdown box.

My model currently looks like this:

<?php
require_once('../Config/config.php');

class AppCalc 
{
    public $dbconn;

    public function __construct()
    {
        $database = new Database();
        $db = $database->dbConnection();
        $this->dbconn = $db;
    }

    public function fillDropdown()
    {
        $stmt = $this->dbconn->prepare("SELECT AppID, Appliance FROM appliances");
        $stmt->execute();
        return $stmt->fetchAll(PDO::FETCH_ASSOC);

        }
    }

?>

My Controller:

<?php
require_once('../Model/applianceModel.php');
require_once('../Tool/DrawTool.php'); 

$newCalc = new AppCalc();
// instantiate drawing tool
$draw = new DrawTool();
// parse (render) appliance view
$renderedView = $draw->render('../View/applianceCalculator.php', array(
    'appliances' => $newCalc->fillDropdown()
));

echo $renderedView;
?>

and my View:

<div id="appForm"> <form id="applianceCalc">
                Typical Appliance: 
                <select name="appliances"> 
                <option value="" disabled selected>Select your option</option>
                <?php foreach($appliances as $appliance): ?> 
                <option value="<?php echo $appliance['AppID']; ?>">
                <?php echo $appliance['Appliance']; ?> </option> <?php endforeach; ?> </select> <br/>

                Power Consumption:
            <input type="text" name="powerConsumption" required="" /> <br/>


            Hours of use per day:
            <input type="text" name="hoursPerDay" required="" /> 
            <br/> 
            </div> 
            <br/> 
            <div id="calcButtons">
            <input type="submit" name="btn-calcApp" value="Calculate"/> <input type="reset" name="btn-calRes" value="Reset"/> 
            </div>
            </form>

And here is the table im working with:

CREATE TABLE IF NOT EXISTS `appliances` (
  `AppId` int(11) NOT NULL AUTO_INCREMENT,
  `Appliance` varchar(50) NOT NULL,
  `PowerConsumption` int(8) NOT NULL,
  PRIMARY KEY (`AppId`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;


INSERT INTO `appliances` (`AppId`, `Appliance`, `PowerConsumption`) VALUES
(1, 'Hairdryer', 1251),
(2, 'Microwave', 1251);

So the code works fine in terms of populating the dropdown with the values from the database, however im struggling with automatically adding a value to the textbox based on the option that is selected in the dropdown.

For example if i was to select "Hairdryer" from the dropdown then the number "1251" should appear in the powerConsumption textbox

It is javascript programming with jquery

$("#textboxID").val( $("#selectID").val())
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.