Use HTML div and store it as a variable

-2

I have a playful web page, in which there are 2 divs, and in a PHP page, I would like to randomize them, the array rand part I already know how to do, the problem is time to get the divs HTML and send them to PHP, here's what I have so far:

<html>
<div id="1">bla bla bla</div>
<div id="2">bla bla bla bla bla</div>
</html>

And now, the part in PHP:

<?php

$var1="div 1 iria aqui";
$var2="div 2 iria aqui";

$random = array('$var1','$var2');

echo $random[array_rand($random)];
    
asked by anonymous 27.10.2015 / 12:30

2 answers

1

Use the javascript to get the id of the div and send it via ajax. This example uses jquery, do not forget to add it in your test / project

<!DOCTYPE html>
<html>
<script type="text/javascript" src="../jquery.js"></script>
<script type="text/javascript">

$(document).ready(function() {
    $("#web").click(function() {
        $.ajax({
            method : "post",
            url : "response.php",
            //data, são informados os campos enviados para o php
            data: {'div1': $("#1").prop('outerHTML'), 'div2' : $("#2").prop('outerHTML'))}
        }).done(function(msg) {
            console.log(msg);
        });
    });
}); 
</script>
<head>
</head>
<body>
   <div id="1">frase 1</div>
   <div id="2">frase 2</div>
   <input type="button" id="web" name="web" value="teste" />
</body>
</html>
    
27.10.2015 / 13:31
2

You can use jQuery for this:

$.post('exemplo.php', {var1: document.getElementById('1').outerHTML});

In PHP:

$var1 = $_POST['var1'];
    
27.10.2015 / 13:31