How to Store PHP variable with HTML in JavaScript in Laravel Blade Template

0 votes

I need a way to store HTML from a PHP variable in JavaScript.

var contents = "{{ $template }}";

This generates an error however. I guess it's because it's not escaped properly, So it messes up the web browser, because JS cannot store it properly... I've tried Laravel escaping

var contents = "{{ e($template) }}";

without any success.

The end goal is: $('#preview-iframe').contents().find('html').html(contents);

How can I accomplish this?

Jun 11, 2020 in Java-Script by kartik
• 37,490 points
5,756 views

1 answer to this question.

0 votes

Hello @kartik,

The double curly brackets {{ }} will always convert special characters to HTML entities. Try using {!! !!} to render the code exactly. However, you will want to make sure you escape the double quotes in the string.

So this:

var contents = "{{ $template }}";

Should be something like:

var contents = "{!! addcslashes($template, '"') !!}";

Hope this works!

answered Jun 11, 2020 by Niroj
• 82,560 points

Related Questions In Java-Script

0 votes
1 answer

How can we access PHP variables in JavaScript or jQuery rather than <?php echo $variable ?>

Hello @kartik, You can also use json_encode for ...READ MORE

answered Apr 29, 2020 in Java-Script by Niroj
• 82,560 points
3,635 views
0 votes
1 answer

How to use React Router with Laravel?

Hii Kartik, Create a route that maps everything ...READ MORE

answered Apr 29, 2020 in Java-Script by Niroj
• 82,560 points
2,428 views
0 votes
1 answer

How to access PHP session variables from jQuery function in a .js file?

Hello, You can produce the javascript file via ...READ MORE

answered Apr 29, 2020 in Java-Script by Niroj
• 82,560 points
4,928 views
0 votes
1 answer

How to get the browser to navigate to URL in JavaScript?

Hii, This works in all browsers: window.location.href = '...'; If ...READ MORE

answered May 28, 2020 in Java-Script by Niroj
• 82,560 points
654 views
0 votes
1 answer

Why it is necessary to refresh CSRF token per form request?

Hello, Generating a new CSRF token for each ...READ MORE

answered Mar 19, 2020 in Laravel by Niroj
• 82,560 points
1,540 views
0 votes
1 answer

What is meant by passing the variable by value and reference in PHP?

Hello, When the variable is passed as value ...READ MORE

answered Mar 27, 2020 in PHP by Niroj
• 82,560 points
286 views
0 votes
1 answer

Connection with MySQL server using PHP. How can we do that?

Hey @kartik, You have to provide MySQL hostname, ...READ MORE

answered Mar 27, 2020 in PHP by Niroj
• 82,560 points
149 views
0 votes
1 answer

How to retrieve or obtain data from the MySQL database using PHP?

Hello kartik,  Actually there are many functions that  ...READ MORE

answered Mar 27, 2020 in PHP by Niroj
• 82,560 points
274 views
+1 vote
1 answer

How to assign Php variable value to Javascript variable?

Hello @kartik, Try: Essentially: <?php //somewhere set a value $var = "a ...READ MORE

answered Jul 7, 2020 in Java-Script by Niroj
• 82,560 points
3,471 views
0 votes
1 answer

How to decode the url in php where url is encoded with encodeURIComponent()?

Hello @kartik, Use this function: <?php $string = 'http%3A%2F%2Fexample.com'; $output = ...READ MORE

answered Jul 7, 2020 in Java-Script by Niroj
• 82,560 points
1,036 views