How to make dynamic display in HTML+PHP?

Asked

Viewed 657 times

0

I’m studying HTML and PHP, but there are things I’m not able to figure out how to do. After all, how do I get input data to process them? Like on the keyboard with those text fields <input type="text">. And, after that, how to adapt the interface of the page or update it with the new interface according to the collected data?

For ease, I put an example where a square matrix size is provided and according to the given value the page interface provides cells to be filled. The gif below illustrates this idea in a very simple way, showing the page changes to each string modification in the "order field".

Gif simples ilustrativo de exemplo de dinâmica de página web em HTML/PHP.

Thanks in advance to those who enlighten me.

  • 1

    This type of modification is done by Javascript, not PHP. What you know about Javascript?

  • to pass the pro PHP variables is usually used $_POST['exemplo'] or $_GET['exemplo'], that takes the value of <input name="exemplo">, as for the matrix, although it is MUCH easier with JS, it is also possible via PHP, through forech and a <table>

  • Thanks. I found out here how to do more or less.

  • only php dynamically without page att is not possible... just using ajax or javascript, as it does not update the page

1 answer

2


I figured out how to do it, without the need for ajax, javascript, table and others. I’ll explain with the example. I made the whole square matrix dynamic interface in the file "index.php".

First, I put the field that should update the interface. Using <form action="index.php" method="post">, the entrance through the field <input name="numeroDeEquações" type="text" /> will reopen the "index.php" page with the variable $_POST['numeroDeEquações'] updated with the value that was passed in the field.

The second and last step is to determine the interface according to the field value. For this, I first check whether $_POST['numeroDeEquações'] defined. I then check if the value is valid. According to the conditions, the interface is built.

Simple as that. Follow the code and thank everyone who tried to help.

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>Matriz Quadrada</title>
    </head>
    <body>

        <form action="index.php" method="post">
            Ordem: &nbsp&nbsp <input name="numeroDeEquações" type="text" /><br />
        </form>

        <?php
            $inválido = true ;
            if( isset($_POST['numeroDeEquações']) ){
                $numeroDeEquações = $_POST['numeroDeEquações'] ;
                if( $numeroDeEquações>1 && $numeroDeEquações<10 ){
                    $inválido = false ;
                    for( $índice1=0 ; $índice1<$numeroDeEquações ; $índice1++ ){
                        for( $índice2=0 ; $índice2<$numeroDeEquações ; $índice2++ ){
                            echo( "<input size=1 type='text' />" ) ;
                        }
                        echo( "<br>" ) ;
                    }
                }
            }
            if( $inválido ) echo( "<br>Forneça um número de 2 a 9." ) ;
        ?>

    </body>
</html>
  • This code works, even with accents in variable names?

  • Yes, if you choose utf-8 text file without good...

Browser other questions tagged

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