Return calculation to <textarea>

Asked

Viewed 791 times

2

I have a PHP page with 2 <textarea> So, I need to do some calculations on the first <textarea> with PHP as soon as the user clicks the button Submit and return the result to 2 <textarea> that’s on the same page, can someone please give me an idea that make it happen?

Important to note that I need to manipulate the text that is inserted in the PHP side, otherwise I have no way to solve the calculations and return them correctly in .

I tried to take the value for $_POST['area_1'] but it did not work, gives error, how can I send to the PHP page the contents of the variable to manipulate it?

  • 2

    What kind of calculation do you need to do? Is it necessary to involve PHP? If yes it will be necessary to use AJAX, otherwise it can be done using Javascript only.

  • I need to do some math calculations with what the user inserts into the first textarea, then present the result in the second textarea, essentially are sums of values within a few FOR loops, nothing too complicated, I don’t know how to work with AJAX, but with javascript know some things...what do you think? P.S. the calculations wanted to solve in PHP on a separate page, type the action form of the first textarea sends to example.php, then the calculations are made and then returns the same to the 2 textarea.... can do that?

  • Yes. Just make the AJAX request with the contents of the first textarea for the example.php, pick up the return and display in the second textarea.

2 answers

2

The functionality you want seems to be possible to do only on the client side (using Javascript). I joined two variants. You can also take a look in this answer to see other ways to send data to the server.

Server-side:

To send to PHP you can do it in different ways. To do how you describe it you need a <form> within which you have the <textare> by name. For example:

<form>
<textarea name="area_1"></textarea>
<input type="submit" value="enviar" />
</form>

In PHP you can capture this value using $_POST['area_1']. Then you can pass that value to a variable, do the calculations you need, and then return it to the client’s side with echo.

$texto_1 = $_POST['area_1'];
$comprimento = 'O comprimento da string foi: '.strlen($texto_1);
echo '
    <form>
    <textarea name="area_1"></textarea>
    <textarea name="area_2">'.$comprimento.'</textarea>
    <input type="submit" value="enviar" />
    </form>
';

On the client side

You can make calculations on the Javascript/Client side if you don’t need to go through the server. In that case you might not even need the <form>.

In that case to have a textarea reference and a button you can do:

var area_1 = document.querySelector('[name="area_1"]');
var area_2 = document.querySelector('[name="area_2"]');
var botao = document.querySelector('button');

botao.onclick = function () {
    area_2.value = area_1.value * 3;
}

jsFiddle: http://jsfiddle.net/4v0rantz/

  • Sergio, I’ll try to do it this way, if I can’t get back here, if it works I mark the answer as right, OK... If anyone else wants to give some ideas will be welcome, thanks in advance for the help (Y)

  • @Luis also added an example with Javascript (client side)

2

In AJAX

   <script>

    $(document).on('click','#enviar',function(){
        $.ajax({
            type: "POST",
            data: { valor : $('#area_1').html() },
            url: "calcular.php",
            success: function(resposta){
                $("#area_2").html(resposta);
            }
        });
    });

    </script>

    <textarea id="area_1"></textarea>
    <input type="button" value="ENVIAR" id="enviar"/>
    <textarea id="area_2"></textarea>

Just make the file calcular.php that will return the value to the second textarea

Browser other questions tagged

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