Work with two different filters in the same location

Asked

Viewed 102 times

0


I’m making some basic algorithms with php, and so of curiosity I decided to make an html with a form, input text and a button to send. in PHP.
If the variable I put in input text is equal to 1, php displays another form below.

I tried to type something in this input text and send, but nothing happened, even though I had created an algorithm that would display the number on the screen.
Follows the code:

<html>

<head><font face="Arial">
</head>
<body>
<form method="POST">
Valor:<input type="text" name="n1"><br>
<input type="submit" name="Enviar" value="Enviar"></form>
<input type="hidden" name="n2" value="0">
<body>
<?php
if(isset($_POST['n1']) || isset($n1))
{
    if(isset($_POST["valor1"]))
    {
        $n1=$_POST['valor1'];
    }   
    else
    {
        $n1 = $_POST['n1']; 
    }
    if(isset($_POST["valor2"]))
    {
        $n2=$_POST['valor2'];
    }   
    else
    {
        $n2 = $_POST['n2']; 
    }
    if($n1 == 1)
    {
    echo '<form method="POST">Valor:<input type="text" name="n2"><br>
<input type="submit" name="Enviar" value="Enviar"></form>
<input type="hidden" name="valor1" value="'.$n1.'">
<input type="hidden" name="valo2" value="'.$n2.'">';
}
if(isset($_POST['n2']) || isset($n2))
{
    echo $n2;
}

}
?>
</html>
  • I cannot identify the variable oftextarea, I haven’t even found a textarea, explains a little more about your doubt and checks if this is all in the example

  • Sorry, I got confused text area with input text. And I wonder if there’s any way to create a form from the php that works

  • which can be read by the algorithm and which displays its value on the screen

1 answer

1


I built from your code an example, there are more direct and less verbose ways to do the same thing, however, I tried to make it more readable.

<html>

<head>
</head>
<body>

    <?php
    $n1 = "";
    if(isset($_POST['n1']))
    {
        $n1 = $_POST['n1'];
    }
    $n2 = "";
    if(isset($_POST['n2']))
    {
        $n2 = $_POST['n2'];
    }

    ?>
    <form method="POST">
        Valor1:<input type="text" name="n1" value="<?php echo $n1; ?>"><br>

        <?php

        if($n1 != "")
        {
            echo 'Valor2:<input type="text" name="n2" value="' . $n2 . '"><br>';
        }
        ?>
        <input type="submit" name="Enviar" value="Enviar">
    </form>
    <hr>
    Valor 1 = <?php echo $n1; ?><br>
    Valor 2 = <?php echo $n2; ?><br>


</body>
</html>
  • I had to correct some parts of the code because where I’m testing it can’t read some commands. But it worked perfectly, thank you

Browser other questions tagged

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