Click here to Skip to main content
16,015,504 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Im working on a small web application that follows a basic MVC pattern. currently i have data being pulled from a database into an array and im echoing out that value into a specific textbox (for now)

HTML
<input type="text" name="txtGasConRes" id="gasConRes" value="<?php echo $typical; ?>" disabled/>


The $typical value is being retrieved from my Model class into my Controller class and then echoed into the textbox for gas. I have a very similar textbox for Electricity.

My dropdown is as follows:
HTML
<select name="heatingType" id="heatingType" required>
          <option value="" disabled selected>Select Your Option</option>
          <option value = "Gas">Gas</option>
          <option value = "Electricity">Electricity</option>
          <option value = "Other">Other</option>          
        </select>


I was wondering if theres a way to determine where to echo the $typical value. So for example, If the user selected Gas as their heating type, then once the form was submitted, the $typical value would appear in the Gas textbox.

If the user selected Electricity from the dropdown then the value would be echoed out into the Electricity textbox.

Any information will be appreciated. Thanks!

What I have tried:

Ive tried a few basic javascript functions but none of them seem to do what i want.
Posted
Updated 18-Apr-16 5:27am
Comments
Sergey Alexandrovich Kryukov 18-Apr-16 10:53am    
This is because you "want" (or rather, think that you want) wrong thing. I'll try to explain, but it's not so easy. I mean, explanation, considering your way of thinking, may be not easy, but the implementation could be easy enough. :-)
—SA

1 solution

Please see my comment to the question.

This is not about determining where to echo. You should never determine that. The whole idea is wrong. This is a bright illustration of the problem we face when developers use one or another design pattern or architectural pattern. This is the case when the use of the pattern clouds the vision of the design itself, which may not happen with people doing the design. The use of patterns is intended to save time on design, but the real effect is misleading.

In your case, you use such a well detailed architectural pattern as MVC (or think that you use it), but get completely lost in much more basic and fundamental design issue, in a way not only totally contradicting to MVC, but to much more basic principles, effectively, to the idea of event-oriented UI in general. You can solve the problem your way, but it would defeat the purpose not only of all patterns, but all the technologies you employ, looking like this: Sergey Alexandrovich Kryukov - Professional Profile - CodeProject[^].

So, please, no offense. I'm writing this only because you need to understand: when you face your problem, your first step should never be solving it. Instead, you first need to realize where you are.

To realize it, it's important to get some design concepts. First of all, this is related to inversion of control: Inversion of control[^].

In tern, this is related to dependency inversion principle, one of SOLID OOP principles:
Dependency inversion principle[^],
SOLID (object-oriented design)[^].

All the event-oriented UI is based on IoC. Armed with conceptual views, look at your UI design. You should not look where to output some data. Instead, you should handle the even of selection of the heating type by the user itself. Roughly put, it should be some JavaScript of this sort:
JavaScript
myHeatingSelector.onselected = // pseudo-code, actual event depends on the UI detail
   function(eventInstance) {
      if (...)
         myTextBox.value = getValueFromSelectedSource;
      else if (...)
         myOtherControl.value = getValueFromSomewhere;
   };

Are you getting the idea? I cannot give you further detail; they depend on your UI detail, this is just the illustration.

But I would rather re-think the idea of using separate text boxes. If a user selects only one heating source at a time, just one output element might be enough. Not only it can make your code more elegant, but the use may get less cluttered page. But it depends, needs some UI design review.

Further decision depends on what you want to get from the server side and when you want to postback. Depending on that you can preload all the page data from the first HTTP response; one known technique is filling in the set of hidden controls on the server side, ugly, but then you operate locally in pure JavaScript, by taking data from one or another control, processing it and showing where you want to see the result.

If you need to get some additional information from the server on each user's selection, you may need to use Ajax to send an HTTP request and retrieve data from HTTP response. In this case, your concern would be the performance of such handling.

And so on…

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900