How to print data from an array

Asked

Viewed 95 times

1

Dear, I have a form that sends data through the method $_POST, check if the data are set $_POST['nome_campo']; and through the foreach(), load the array $_POST inside of a variable! After performing the foreach() I would like to know how to print each array value in another form!?

<?php       

   if( isset($_POST)){         

        $txtQtdFunc = $_POST['txtQtdFunc'];

        //print_r($_POST);

        for ($i = 0; $i < $txtQtdFunc; $i++) {

            //Recebe todos os campos e registros do form

            foreach($_POST as $nome_campo => $valor){

               echo $strMsg = $valor ."<br>";     

           }//fim foreach    
  }//fim isset            

?> 
  • I believe "$_POST" is missing the variable, there at foreach time.

  • It would be important to also post your form HTML so that we can verify how the information is being sent, because name = "txtQtdFunc", is not a good choice. It is not possible to know if it is a text, a number or some data of the Employee. Writing simple and objective names within your application makes it easier to understand and behave your application.

1 answer

0

There’s an error in the line:

if(isset($_POST)) {

According to the function documentation isset:

Checks whether the variable is set.

If the variable is destroyed with unset(), she will no longer exist. isset() return FALSE if used in a variable with the NULL value. Remember that in PHP a NULL byte (\0) is different from the constant NULL.

The variable $_POST is always started. This way, this condition is being misused.

Regarding the description of your problem, it was not very clear, but I worked out a possible solution:

<?php       
  if(isset($_POST['txtQtdFunc'])) { // Valor que deve vir via post
    $txtQtdFunc = $_POST['txtQtdFunc'];

    // Imprimir valores
    foreach($_POST as $chave => $valor) {
      echo "$chave - $valor"; // Imprimirá todas as chaves e seus respectivos valores
    }
  }
?> 

Thus, all values sent via post will be shown.

  • Ok, that way it worked! Now I’d like to know how to print the values of $value in an input in html form

Browser other questions tagged

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