0

So, a quick breakdown.

I've got a function on my page loads three sections, currently it loads them separately (which is kind of slow). So I've recently combined them all. I now have all of the information stored in PHP variables. I'd like to get those PHP variables and load them into separate divs.

here is an example of what I'd like to do.

HTML

<div class="one"></div>
<div class="two"></div>
<div class="three"></div>

PHP

$var1 = "<div><p>data 1</p></div>";
$var2 = "<div><p>data 2</p></div>";
$var3 = "<div><p>data 3</p></div>";

jQuery

//`get var1,var2,var3`
// load var1 into div.one
// load var2 into div.two
// load var3 into div.three

The current problem I'm having is json_encode is changing some information into this "\n\t\t\t\t\t\t" and it's printing

"<div><p>data 1</p></div>"

instead of "data 1" in a p inside of a div.

2
  • Welcome to SO. In your script you can do this: var phpDiv = "<?php echo $var1; ?>"; Commented Apr 27, 2015 at 14:31
  • whitespace characters that are added by json_encode don't matter to how it's rendered in html. It seems you are using jQuery.text() instead of jQuery.html() Commented Apr 27, 2015 at 14:41

2 Answers 2

1

Try:

<div class="one"></div>
<div class="two"></div>
<div class="three"></div>

<script>
var var1 = "<?php echo json_encode($var1); ?>"; 
var var2 = "<?php echo json_encode($var2); ?>"; 
var var3 = "<?php echo json_encode($var3); ?>"; 

$(".one").html(var1);
$(".two").html(var2);
$(".three").html(var3);
</script>

You can pass PHP to JS in this way, but not the other way around.

Sign up to request clarification or add additional context in comments.

Comments

0
var var1 = "<?php echo json_encode($var1); ?>"; 
var var2 = "<?php echo json_encode($var2); ?>"; 
var var3 = "<?php echo json_encode($var3); ?>"; 

Comments

Your Answer

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