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;
}
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.
– Rodrigo Rigotti
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?
– Luis
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.
– Rodrigo Rigotti