How to read what the user typed in a form?

Asked

Viewed 1,148 times

3

The code is like this:

<form method="post" action="pagina.php"/>
<?php
$c= 1;
$neg = 0;
    while($c <=3){
        echo "Digite um numero: <input type='number' name='$num' min='1'/>";
            if($num < $neg){
                $total++;
                }
            $c++;
            }
?>
<input type='submit' value='Resultado'/>

I’m not sure how to do what the user typed, it becomes a variable so I can use the control structure on it. I tried putting a direct variable in the name ($num), but it doesn’t seem to work or it doesn’t work. Or this kind of thing is for Javascript ?

  • 5

    I would suggest reading the introductory pages of the PHP manual. Your code makes no sense. PHP is not a language that naturally interacts with user when used via HTML and browser. PHP only assembles static things. The client receives the content, and if he wants to interact, he has to call a new PHP (or the same, but in a new execution). Start here, with attention to detail: http://us3.php.net/manual/en/getting-started.php - If you are easy with English, better still, the documentation in Portuguese is of less quality (but for the basics it is already good)

  • 1

    If you want to know what is written in the form, just use javascript, $("form"). val(); If you want to take php, just send the form via ajax or to a page and turn into variable, using POST or GET methods

2 answers

4


A simple example of a dynamically generated form with PHP:

pagina_um.php

<form method="post" action="pagina_dois.php">
<?php
    for( $c = 1; $c <= 3; ++$c) {
        echo "   Digite um numero: <input type='text' name='input_$n'>";
    }
?>
   <input type='submit' value='Resultado'>
</form>

page_two.php

<?php
    $total = 0;
    for( $c = 1; $c <= 3; ++$c) {
        $total = $total + $_POST["input_$n"];
    }
    echo "O total é $total";
?>

Note that there are two steps. The first, generating the form. The second, receiving the result of the form.

I didn’t do any protection to check if the data makes sense, because it’s just an elementary example. In a practical application, you need a lot of extra care.

Basically, when you have one form methodically POST, have to pick up using the global variable $_POST['nomedocampo'].

1

If you want to pick up what the user typed in pagina.php, just use PHP’s GET or POST methods! As you declared the POST method in the form, just call the function $_POST[''] in pagina.php.

You can also do this with Javascript, but on the same page, creating intervals of a few seconds, you can create a preview of the text the user made.

  • Tip

Remove php code from your form, it doesn’t make sense that code! And create a input normal in HTML, and name, place of your preference. As "number" for example.

Examples

PHP (page.php)

$number = $_POST['number'];
echo ($number);
//vai aparecer o que o usuário digitou ao clicar no Resultado.

Javascript (form page)

$("#butaum").click(function() {

  var number = $('#number').val();

  $("#resultado").html(number);

});

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form>
  <input type='number' id="number" name='number' min='1'>
</form>

<button id="butaum" >Resultado</button>

<div id="resultado">


</div>

Browser other questions tagged

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