1

I want to update two divs with one ajax call with json encode outside php marks. I am familiar with Update two divs with one AJAX response but would it be possible to do something similar but with data outside < php ?>

Instead of echo-ing ajax response i've closed php and used regular html like for example here is code server side:

<?php
//here some code that cheks some stuff
var $somvariable="Ipsum";
?>
<p>Lorem Lorem <?php echo $somvariable ; ?></p>

and then in ajax function I update div like:

success:function(data, textStatus, jqXHR) 
{
//data: return data from server
$('#divid').html(data);
}

Now imagine i have to use json_encode to create two variables that update two divs so i can do:

success:function(data, textStatus, jqXHR) 
{
$('#dividone').html(data.one);
$('#dividtwo').html(data.two);
}

and my code outside < ?php ?> is to big and would be too time consuming to convert it to normal string. How can I make this work? My guess is use raw json but how? obviously nothing like:

<?php
echo json_encode(array(
    'one' => '?><p>Lorem Lorem <?php echo $somvariable ; ?></p><?php',
    'two' => '?><p>Lorem <?php echo $somvariable2 ; ?></p><?php'
));
?>

Will not work but I hope it shows what i try to accomplish.

2 Answers 2

1

may be you need to set header in php

header('Content-Type: application/json');

so your php code should be something like this

<?php
header('Content-Type: application/json');
echo json_encode(array(
    'one' => '<p>Lorem Lorem '.$somvariable.'</p>',
    'two' => '<p>Lorem  '.$somvariable2.'</p>'
));
?>

and in ajax use

success:function(data) 
{
   $('#dividone').html(data.one);
   $('#dividtwo').html(data.two);
}
Sign up to request clarification or add additional context in comments.

3 Comments

That is interesting - could you provide example of simple json with two variables created this way? so something like <?php header('Content-Type: application/json'); ?> and then how to format variables?
@GrzegorzZieliński I actually use it in my code in this way .. but I don't have a live example to it .. just replace it with your code and test it .. and see updated answer
The problem is <p>Lorem Lorem <?php echo $somvariable ; ?></p> really is a much bigger html / php code. Thats why it would be time consuming to convert it to normal php string like in your response. That's why I'm trying to find a way to do it outside php - perhaps in raw json.
0

With some research I've managed to figure this out. ob_get_clean() and ob_start() is what I needed.

<?php ob_start(); ?>
<div>First huge mix of php and html goes <?php echo "here"; ?></div>
<?php $one= ob_get_clean(); 
ob_start(); ?>
<div>Second huge mix of php and html goes <?php echo "here"; ?></div>
<?php $two= ob_get_clean();
echo json_encode(array(
  'one' => $one,
  'two' => $two
)); ?>

and in ajax I used

success:function(data) 
{
$('#dividone').html(data.one);
$('#dividtwo').html(data.two);
}

Comments

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.