0

when i try dd($conv) the data are there, but I still can't display them inside the script. Anyone know whats the problem, I have tried many ways but they doesn't seem to work.

  <h4>{{$row->transcript_text}}</h4>
    <?php $conv = json_encode($row->transcript_text); ?>
    <script type="text/javascript">
        function splitArray() {
            var myStr = <?php echo $conv; ?>;
            var strArray = myStr.split(/(?= \| \d)/);

            // Display array values on page
            for (var i = 0; i < strArray.length; i++) {
                $("body").addClass('col-lg-offset-1').append("<h4>" + strArray[i] + "</h4>");
            })
        }
        splitArray();
    </script>
10
  • does it give any error in the console ? If not, what's the generated HTML ? Commented Mar 6, 2017 at 22:21
  • Thanks for asking ValLeNain, No error no data, I just got the blank page. If I do it another way I can display the data (meaning they are there) but I need to split those therefore I have to go this way. Commented Mar 6, 2017 at 22:25
  • no error, no data ? And you're sure $history is defined, not empty and iterable ? Commented Mar 6, 2017 at 22:28
  • yes, because I can display them outside the script Commented Mar 6, 2017 at 22:30
  • 1
    This code ends up with multiple bodies. Commented Mar 7, 2017 at 5:57

1 Answer 1

1

I don't know which version of Laravel you're using but the recommended way to embed plain PHP in your HTML templates is with the @php notation (cf doc). Anyway, that's something I'd try to avoid. Instead, Laravel blade has native ways of doing what you're trying to do.

@if(! empty($history))
    @foreach($history as $row)
    <script type="text/javascript">
        function splitArray() {
            var myStr = JSON.parse("{{json_encode($row->transcript_text) }}");
            var strArray = myStr.split(" | 0");

            // Display array values on page
            for (var i = 0; i < strArray.length; i++) {
                $("body").addClass('col-lg-offset-1').append("<h4>" + strArray[i] + "</h4>");
            }
        }
    </script>
    <body onload="splitArray()" style="color: lime">
    </body>
    @endforeach
Sign up to request clarification or add additional context in comments.

1 Comment

I am using laravel 4.2 but this solution does not work, still getting just en emptu page

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.