PHP, HTML FORM, how to interact with values in forms?

Asked

Viewed 149 times

0

I’m venturing a little with PHP, and I came across the following situation.

<div class="form-group">
    <label for="recipient-name" class="control label">Tipo: </label>
    <select name="teste" class="form-control">
      <option value=""></option>
      <option value="1">Valor 1</option>
      <option value="2">Valor 2</option>
     </select>
</div>

Is there any way to get that value I’m assigning to select, and assign a variable in that way:

<?php $a = $_POST["teste"];?>

And use this variable $a elsewhere?

  • Hello, welcome to Sopt! You tried to do this by yourself?

  • Yes, I’m just venturing a little bit with php to see how far it goes... and I came across this. it is possible to do, or there is something different in php q do it?

  • or there just happens to be some other way of doing it for another language?

  • or if you don’t have, you just happen to be able to restrict questions from another select depending on that one?

  • 1

    You are saying 'save' this variable for later access?

  • Of course: session, cookie, asynchronous requests, cache. Here it seemed to me that you want to manipulate the form before sending. If that’s the case, you need to Javascript.

  • Yes, in the case... what I look for in specific javascript?

Show 2 more comments

1 answer

0

Hello.

I’m not sure I quite understand your question. But the basic HTML + PHP interaction would be to put select inside a form, with the action pointing to the page that will contain the variable you want to use, and with the method that can be POST or GET. For example:

Page_do_formulario.html

  <form action="pagina_da_variavel.php" method="POST">
    <div class="form-group">
        <label for="teste" class="control label">Tipo: </label>
        <select name="teste" class="form-control">
          <option value=""></option>
          <option value="1">Valor 1</option>
          <option value="2">Valor 2</option>
        </select>
    </div>
  </form>

pagina_da_variavel.php

<?php

if(!empty($_POST['teste']){
  $a = $_POST['teste'];
}
else{
  $a = "Nada foi selecionado no select";
}

echo $a;

?>
  • hello, I do not want to observe my data after a Ubmit, I would like to observe it before submitting to use it as a restriction for another select p.ex.

  • Then your question must be edited to make you understand that you want to manipulate the form before sending. This assignment you did (<?php $a = $_POST["teste"];?>) only happens after sending.

Browser other questions tagged

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