Screen to enter values

Asked

Viewed 83 times

1

I have this code and I would like to create a screen for the user to enter values and they are inserted in their answers arrays, CREDITO (array a) and DEBITO (array b) and create a button to run the code and print the answer. Could you help? I don’t know about ajax, javascript... I’m struggling, I imagine it’s easy, but I’m not getting it.

Follows code.

<?php

$a = [];
$b = [];

$iguais = [];
$diferentes = [];
foreach($a as $item){
    $i = array_search($item, $b);
    if($i !== false){
        $iguais[] = '('. $item .', '. $b[$i] .')';
        unset($b[$i]);
    }else{
        $diferentes[] = $item;
    }
}

$d = [$b];
$e = [$diferentes];

$common = array_intersect( $diferentes, $b,$a); 

$diff2 = array_diff( $b, $common );
$diff3 = array_diff( $diferentes, $common );

$diferentes += $b;

echo "IGUAIS - ";
print_r($iguais);

echo "DEBITO - ";
print_r($diff3);

echo "SOMA(a) = ".array_sum($diff3)."\n";

echo "CREDITO - ";
print_r($diff2);

echo "SOMA(a) = ".array_sum($diff2)."\n";

?>

inserir a descrição da imagem aqui

Thank you.

  • 1

    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

  • 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?

  • 1

    Yes, I understand, it’s something very simple, I’ll make an example

  • Thank you @lvcs !

  • Thanks for the tip, already edited and excludes the other comment!

2 answers

1

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";

}

?>
  • Good morning @lvcs , I tried to run here did not work.

0

In your html, the inputs must be in a form tag to work. What we need to focus on is the "action" and "method" properties of the form tag, the "name" property of the text input tags (credit and debit) and the "type" property of the EXECUTE input need to be defined as "Submit".

That is, the part of the HTML form would look like this:

<form action="endereco/do/seu/script.php" method="post">
   <label>DEBITO: </label><input type="text" name="debito" />
   <label>CREDITO: </label><input type="text" name="credito" />
   <input type="submit" value="EXECUTAR" />
</form>

And you can fetch these values in your so-called "script.php" by changing the first 4 lines of code to:

<?php
$a = [];
$b = [];
$a[] = $_POST['credito'];
$b[] = $_POST['debito'];

Browser other questions tagged

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