1

This PHP code outputs HTML to the user.

    <div class="whole">
        <input type="hidden" class="hid" name="hid" value="<?php echo $datum['id']; ?>">
        <div class="col1">  <div class="d1" id="naam<?php echo $datum['id']; ?>"><?php echo $datum['name']; ?></div>        
        </div>
        <div class="col2">  <div class="d2" id="email<?php echo $datum['id']; ?>"><?php echo $datum['email']; ?></div>

        </div>
        <div class="col3">  <div class="d3" id="state<?php echo $datum['id']; ?>"><?php echo $datum['state']; ?></div>

        </div>
        <div class="col4">  <div class="d4" id="gender<?php echo $datum['id']; ?>"><?php echo $datum['gender']; ?></div>

        </div>
        <div class="col5">  <div class="d5" id="lang<?php echo $datum['id']; ?>"><?php echo $datum['lang']; ?></div>

        </div>
        <div class="col6">
            <?php
                echo "<a href='images/".$datum['image']."' data-lightbox='picbox' data-title='".$datum['image']."'> <img src='images/".$datum['image']."'></a>";
            ?>
        </div>
    </div>

Instead of sending this output to the user, I want it in the variable $output. How can I do this?

wizzwizz4
  • 6,140
  • 2
  • 26
  • 62
Neo
  • 11
  • 2

3 Answers3

0

You can use Jquery to get HTML code inside your div:

$( document ).ready(function() {
    data = $(".whole").html();
    console.log(data);
});

Where data is:

<input type="hidden" class="hid" name="hid" value="<?php echo $datum['id']; ?>">
<div class="col1">  <div class="d1" id="naam<?php echo $datum['id']; ?>"><!--?php echo $datum['name']; ?--></div>
</div>
<div class="col2">  <div class="d2" id="email<?php echo $datum['id']; ?>"><!--?php echo $datum['email']; ?--></div>

</div>
<div class="col3">  <div class="d3" id="state<?php echo $datum['id']; ?>"><!--?php echo $datum['state']; ?--></div>

</div>
<div class="col4">  <div class="d4" id="gender<?php echo $datum['id']; ?>"><!--?php echo $datum['gender']; ?--></div>

</div>
<div class="col5">  <div class="d5" id="lang<?php echo $datum['id']; ?>"><!--?php echo $datum['lang']; ?--></div>

</div>
<div class="col6">
    <!--?php
        echo "<a href='images/".$datum['image']."' data-lightbox='picbox' data-title='".$datum['image']."'--> <img src="images/&quot;.$datum[" image']."'="">";
    ?&gt;

After that you can send data to PHP via AJAX

MorganFreeFarm
  • 3,811
  • 8
  • 23
  • 46
0
<?php
ob_start();
?>
    your html goes here
<?php
$html = ob_get_contents();
ob_end_clean();
lukistar
  • 58
  • 1
  • 7
  • It'd help if you explained why this code works. Also, why didn't you just use a heredoc? – wizzwizz4 Jul 08 '19 at 14:06
  • https://stackoverflow.com/questions/13455517/php-vs-ob-start Heredoc is just ugly I suppose(for me) – lukistar Jul 08 '19 at 14:40
  • So why don't you use `ob_get_clean`? https://stackoverflow.com/q/17792817/5223757 – wizzwizz4 Jul 08 '19 at 14:52
  • 1
    There are so many ways to do the same thing in php, it really doesn't matter how you do stuff as long as they work and are understandable... – lukistar Jul 10 '19 at 13:35
0

Use heredoc syntax, like so:

$output = <<<ANY_IDENTIFIER_THAT_DOES_NOT_APPEAR_IN_THE_TEXT_HERE
<div class="whole">
    <input type="hidden" class="hid" name="hid" value="{$datum['id']}">
    <div class="col1">  <div class="d1" id="naam{$datum['id']}">{$datum['name']}</div>        
    </div>
    <div class="col2">  <div class="d2" id="email{$datum['id']}">{$datum['email']}</div>

    </div>
    <div class="col3">  <div class="d3" id="state{$datum['id']}">{$datum['state']}</div>

    </div>
    <div class="col4">  <div class="d4" id="gender{$datum['id']}">{$datum['gender']}</div>

    </div>
    <div class="col5">  <div class="d5" id="lang{$datum['id']}">{$datum['lang']}</div>

    </div>
    <div class="col6">
        <a href='images/{$datum['image']}' data-lightbox='picbox' data-title='{$datum['image']}'> <img src='images/{$datum['image']}'></a>
    </div>
</div>
ANY_IDENTIFIER_THAT_DOES_NOT_APPEAR_IN_THE_TEXT_HERE;

This last part must be exactly the same identifier as at the beginning, unindented and followed by a semicolon.

Also, note that I took out all of the <?php ?> sections, and replaced them with {$datum['image']} accesses (unless I missed one). Heredocs in PHP work just like "double quoted strings", except spread across multiple lines and able to include " marks.

For more information, see the PHP documentation on string syntax.

wizzwizz4
  • 6,140
  • 2
  • 26
  • 62