0

In an ajax on success function I am receiving some value and I want to pass that value into a div

here is the div in blade view where I want to pass value

<div id = "demo">
</div>

here is my javascript code where I am receiving values

            success: function (data) {
            var getsellerreport = data.getsellerreport;
            var output = '<tbody>
            @foreach($getsellerreport as $preport)
              <tr>
                <td>{{ $preport['product_id'] }}</td>
                </tr>
           @endforeach
           </tbody>';
            document.getElementById("demo").innerHTML = output;
             }

According to this code I am using output= 'html' to store html and trying to pass output in demo but it is not working.

Can someone help me? how can I pass html from script to blade view?

1 Answer 1

1

The thing is Blade is not what you can use in JavaScript or HTML directly. It won't work. If you need them in JavaScript, you need to write code in JavaScript way.

Here's an example how it can work with mock data.

$(document).ready(function() {
  $.get({
    url: 'https://gorest.co.in/public-api/users',
    success: function (data) {
      // var getsellerreport = data.getsellerreport;
      var getsellerreport = [{
        product_id: 1, product_name: 'Product 1',
      }, {
        product_id: 2, product_name: 'Product 2',
      }, {
        product_id: 3, product_name: 'Product 3',
      }];
      var output = `<tbody>
        ${getsellerreport.map(function(preport) {
          return `<tr><td>${preport.product_id} - ${preport.product_name}</td><tr>`;
        }).join('\n')}
      </tbody>`;
      document.getElementById("demo").innerHTML = output;
    }
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table id="demo">
</table>

If you need production id and name at the same time then

$(document).ready(function() {
  $.get({
    url: 'https://gorest.co.in/public-api/users',
    success: function (data) {
      // var getsellerreport = data.getsellerreport;
      var getsellerreport = [{
        product_id: 1,
      }, {
        product_id: 2,
      }, {
        product_id: 3,
      }];
      var output = `<tbody>
        ${getsellerreport.map(function(preport) {
          return `<tr><td>${preport.product_id} - ${preport.product_name}</td><tr>`;
        }).join('\n')}
      </tbody>`;
      document.getElementById("demo").innerHTML = output;
    }
  });
});
Sign up to request clarification or add additional context in comments.

2 Comments

i am also getting product name using model preport->product->name do u have any experience how i can gat taht product name into script?
no problem. just do whatever you want written inside of map function.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.