From what I understand you want to go putting data in the array and run your code, I used session variables to store the arrays even when the page is updated (they are only deleted when the browser is closed or the session is destroyed). I felt the need to put 3 actions in the system:
- Add: just add the input value to your array.
- Execute: executes the code of the function.
- Close: destroys the session and resets the arrays.
Of course the code can be improved, but I saw no need to change it further. Although I could not understand the purpose of it.
<!-- FORMULARIO DE ADICIONAR -->
<form action="#" method="post">
<p><input name="number" placeholder="Crédito" type="text"></p>
<p><input name="number" placeholder="Débito" type="text"></p>
<p><input type="submit" value="ADICIONAR"></p>
</form>
<!-- FORMULARIO DE EXECUTAR -->
<form action="#" method="post">
<p><input name="executa" type="hidden"></p>
<p><input type="submit" value="EXECUTAR"></p>
</form>
<!-- FORMULARIO DE ENCERRAR -->
<form action="#" method="post">
<p><input name="encerrar" type="hidden"></p>
<p><input type="submit" value="ENCERRAR"></p>
</form>
<?php
/**
* Created by PhpStorm.
* User: Leonardo Vilarinho
* Date: 07/04/2016
* Time: 16:31
*/
session_start();
// quando formulario de executar foi enviado
if(isset($_POST['executa']))
{
// executa função de calcular
calcula($_SESSION['credito'], $_SESSION['debito']);
}
// quando formulario de encerrar foi enviado
if(isset($_POST['encerrar']))
{
// destroi arrays e a sessão
unset($_SESSION);
session_destroy();
}
// quando formulario de adicionar foi enviado com o credito
if(isset($_POST['credito']))
{
// adiciona valor do input no array
array_push($_SESSION['credito'], $_POST['credito']);
}
else
{
// quando for a primeira vez que entrou na pagina cria um array
$_SESSION['credito'] = array();
}
// quando formulario de adicionar foi enviado com o debito
if(isset($_POST['debito']))
{
// adiciona valor do input no array
array_push($_SESSION['debito'], $_POST['debito']);
}
else
{
// quando for a primeira vez que entrou na pagina cria um array
$_SESSION['debito'] = array();
}
// exibe os arrays
var_dump($_SESSION);
// tranforme ie função para ficar légivel
function calcula($credito, $debito)
{
$iguais = array();
$diferentes = array();
foreach($credito as $item){
$i = array_search($item, $debito);
if($i !== false){
$iguais[] = '('. $item .', '. $debito[$i] .')';
unset($debito[$i]);
}else{
$diferentes[] = $item;
}
}
$common = array_intersect( $diferentes, $debito,$credito);
$diff2 = array_diff( $debito, $common );
$diff3 = array_diff( $diferentes, $common );
$diferentes += $debito;
echo "IGUAIS - ";
print_r($iguais);
echo "DEBITO - ";
print_r($diff3);
echo "SOMA(credito) = ".array_sum($diff3)."\n";
echo "CREDITO - ";
print_r($diff2);
echo "SOMA(credito) = ".array_sum($diff2)."\n";
}
?>
I did not understand the question very well, but it does not seem necessary to use Javascript and much less Ajax. Only one HTML interface with two inputs, input credit and input debit
– Leonardo
Then, the HTML interface ta ready I do not know how to receive the values in credit and debit and they are inserted in the array a and in the code array b. After inserted they run the code and printed the result on the screen. Managed to understand?
– Gustavo Freire
Yes, I understand, it’s something very simple, I’ll make an example
– Leonardo
Thank you @lvcs !
– Gustavo Freire
Thanks for the tip, already edited and excludes the other comment!
– Gustavo Freire