Use HTML div and store it as a variable

Asked

Viewed 998 times

-2

I have a playful web page, on which there are 2 Ivs, and on a PHP page, I would like to add a word to them, the array rand I already know how to do, the problem is time to take the HTML Divs 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 PHP part:

<?php

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

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

echo $random[array_rand($random)];
  • 1

    Welcome to Sopt, do the [tour] and read the [Ask]

  • 2

    Your question is very vague, which makes it incomprehensible. It is better to edit it by providing more information. I suggest to specify better what type of treatment and model you want.

  • You want to <div id="1">bla bla bla</div> in the variable or bla bla bla?

  • Use javascript to take the content of div and send it by ajax to php.

  • 2

    @Jorgeb. I want to take the whole div.

  • @rray you could post an example here?

Show 1 more comment

2 answers

2

You can use jQuery for that:

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

In PHP:

$var1 = $_POST['var1'];

1


Use javascript to grab the div id and send it by ajax. This example uses jquery, don’t forget to add it to 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>
  • He doesn’t just want the content...

  • @Jorgeb. I’ll edit then, worth the warning :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.