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.

4
  • which value you want fetch from database? and based on color selection. Commented Aug 30, 2017 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
    Commented Aug 30, 2017 at 12:25
  • I post answer please check. Commented Aug 30, 2017 at 12:38
  • If it help you, then mark as solution so it help other in community as well. Commented Aug 30, 2017 at 12:45

1 Answer 1

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"
    }
}
7
  • 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
    Commented Aug 31, 2017 at 4:11
  • Have you not create custom model in your extension? Using that you can fetch the value from your table. Commented Aug 31, 2017 at 12:43
  • No i didnt do that just module
    – Hemal
    Commented Sep 1, 2017 at 8:28
  • Without model it is not possible to get data from database. So you need to create model into your custom extension. Commented Sep 1, 2017 at 8:35
  • can u help me with that if you can
    – Hemal
    Commented Sep 1, 2017 at 8:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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