0

I have drop down call color below code represent that

<!-- drop down for color -->

<p><br>Frame Color</p>
<select id="color" onchange="Calculation();">
<option value="White">White</option>
<option value="Red">Red</option>
<option value="Green">Green</option>
<option value="Black">Black</option>
<option value="Wood limitation">Wood limitation</option>
</select>

<b><p id="framecolor"></p></b>

If someone select value i will change the label using java scripts

//frame color calculation
   var color = document.getElementById('color').value;

        if(color=="White"){
            framecolor = (0);
            document.getElementById("framecolor").innerHTML = "FrameColor Price : " + framecolor;
        }
        else if(color=="Red"){
            framecolor = (4);
            document.getElementById("framecolor").innerHTML = "FrameColor Price : " + framecolor;
        }
        else if(color=="Green"){
            framecolor = (8);
            document.getElementById("framecolor").innerHTML = "FrameColor Price : " + framecolor;
        }
        else if(color=="Black"){
            framecolor = (12);
            document.getElementById("framecolor").innerHTML = "FrameColor Price : " + framecolor;

I have created a database table call 'color_price' like below

id      color     price_component
1       white     0
2       red       4
3       green     8
4       black     12

I have add database table using InstallSchema and the script code and dropdown in frontend/templates/test.php file

If someone select color how can i retrieve data from database table instead of using hard coding.

  • which value you want fetch from database? and based on color selection. – Dhiren Vasoya Aug 30 '17 at 12:22
  • I want to retrieve the price_component according to what color user select from drop down. I have done it by hard coding you can see that in above script code – Hemal Aug 30 '17 at 12:25
  • I post answer please check. – Dhiren Vasoya Aug 30 '17 at 12:38
  • If it help you, then mark as solution so it help other in community as well. – Dhiren Vasoya Aug 30 '17 at 12:45
3

You need to solve your problem using Ajax request like this.

function Calculation()
{
    var formurl = '<?php echo $this->getUrl('extension/controller/action'); ?>';
    var selectedval = $('#color').val();

    jQuery.ajax({
          type: "GET",
          url: formurl,
          data: {isAjax: 1, id: selectedval}, 
          success: function(transport) 
          {
              var response = transport;
              document.getElementById("framecolor").innerHTML = "FrameColor Price : " + response;
          }
     });
}

Now in your controller action you need to do like this :

class Action 
{
    public function execute()
    {
         $color = $this->getRequest()->getParam('id');

         //Write Your model code to fetch value from database using color and return "price_component"
    }
}
  • can u please tell me how to fetch value from database as u said in comment. Im new to magento please help me with this – Hemal Aug 31 '17 at 4:11
  • Have you not create custom model in your extension? Using that you can fetch the value from your table. – Dhiren Vasoya Aug 31 '17 at 12:43
  • No i didnt do that just module – Hemal Sep 1 '17 at 8:28
  • Without model it is not possible to get data from database. So you need to create model into your custom extension. – Dhiren Vasoya Sep 1 '17 at 8:35
  • can u help me with that if you can – Hemal Sep 1 '17 at 8:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.