How to get the value of a field on the same page

Asked

Viewed 1,444 times

2

I have a form field on a page called register.php, for example. So I want to create a variable that receives the value that the user enters in this field, but I want this variable to be in the same code and not in another page, but I don’t know how to get this value, because it is not being passed either via post or via get. Has as?

like I know it’s not like that, but I’ll set this example for those who want to answer.

field: <input type="text" name="nome"/>

then I want to extract for a variable that will be on the same page of the code the value that the user enters...

$nome = $_GET['nome'];. I know it’s not like that, but how do I do it? Or is there no way?

  • You want pagination or not?

  • 1

    With PHP there is no way, if I understand what you want to do, but with Javascript is possible.

1 answer

2


Just use the action="" empty and do so:

<?php
if (isset($_GET['nome'])) {
    $nome = $_GET['nome'];

    echo 'Olá', htmlentities($nome), '!';
}
?>
<form method="get" action="">
Nome: <input type="text" name="nome">
<button>Enviar</button>
</form>

This way the request goes to "same" page as you send the form.

But of course it will page, even if it was POST, if what you want is to update a variable without page, there is no way, because Web is HTTP and HTTP works like this:

htt

That is always have to have request and reply, however you can use Ajax and maybe combine with session, depends a lot of what you really want to do.

Browser other questions tagged

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