Pass value to another page

1

I have a half basic question, I want to pass a value from one page to another and are several divs with plans and would have to receive one of these plans: "basic", "medium", "advanced". You are thus div :

<div class="col-md-3">
 <input type="submit" name="basic" value="Choose this Plan" class="btn btn-primary"> </div>

<div class="col-md-3">
     <input type="submit" name="medio" value="Choose this Plan" class="btn btn-primary"> 
     </div>

<div class="col-md-3">
     <input type="submit" name="avancado" value="Choose this Plan" class="btn btn-primary"> 
     </div>

How can I pass the value from one page to another?

    
asked by anonymous 07.07.2017 / 07:53

4 answers

2

To send the values to a php page you can include a form and specify the page in the action attribute of the form.

The choice of plan can thus be made in a selection box, built using the select tag.

<form action="pagina.php" method="POST">

  <div class="col-md-3">
    Plano 
    <select name="plano">
      <option value="basic">Basico</option>
      <option value="medio">Médio</option>
      <option value="avancado">Avançado</option>
    </select>
  </div>
  
  <input type="submit" value="Enviar">
</form>
    
07.07.2017 / 11:19
0

Even following what you posted, you have two ways, one would be to go through a form, another to use jQuery (but I do not think that's what you want). Follow the example

    <form action="outraPage.php" method="POST">
    <div class="col-md-3">
     <input type="submit" name="basic" value="Choose this Plan" class="btn btn-primary"> </div>

    <div class="col-md-3">
         <input type="submit" name="medio" value="Choose this Plan" class="btn btn-primary"> 
         </div>

    <div class="col-md-3">
         <input type="submit" name="avancado" value="Choose this Plan" class="btn btn-primary"> 
         </div>
    </form>

    // Outra Page
<?php
  echo $_POST['avancado'];
  echo $_POST['medio'];
  echo $_POST['basic'];
    
07.07.2017 / 17:46
0

To get to the PHP page you can use it like this (since name= changes):

<?php
//Verifica os valores que foram enviados
if (!empty($_POST['basic']) {
    $plano = 'basico';
} else if (!empty($_POST['medio'])) {
    $plano = 'medio';
} else if (!empty($_POST['avancado'])) {
    $plano = 'avancado';
} else {
    die('Escolha um plano'); //Finaliza o PHP e emite erro
}

echo 'Você escolheu o plano: ', $plano;
  

If you are using GET instead of POST, change all $_POST to $_GET

On HTML, no problem with using more than <input type=submit> , the browser will recognize the submit button you used and, when clicked, it changes the value and the "key" of the request, for example this:

<form id="meuform" method="POST" action="pagina">

    <div class="row">

        <div class="col-md-3">
            <input type="submit" name="basic" value="Choose this Plan" class="btn btn-primary">
        </div>

        <div class="col-md-3">
            <input type="submit" name="medio" value="Choose this Plan" class="btn btn-primary">
        </div>

        <div class="col-md-3">
            <input type="submit" name="avancado" value="Choose this Plan" class="btn btn-primary">
        </div>

    </div>

</form>

Clicking on "basic" looks at how the browser sent:

Byclicking"advanced":

Extra:form=attributeinHTML5

Onesuggestionistousetheform=attribute(whichisonlyavailablewithbrowsersthatsupportHTML5,butforolderbrowsersyoucanuseapolyfill).

Whatyoushoulddofirstistoputtheinputsoftypesubmit"out" of <form> and set an id in this form, an example id="meuform" , should be more or less like this:

<form id="meuform">
...
</form>

<div class="col-md-3">
    <input form="meuform" type="submit" name="basic" value="Choose this Plan" class="btn btn-primary">
</div>

<div class="col-md-3">
     <input form="meuform" type="submit" name="medio" value="Choose this Plan" class="btn btn-primary"> 
</div>

<div class="col-md-3">
     <input form="meuform" type="submit" name="avancado" value="Choose this Plan" class="btn btn-primary">
</div>
    
07.07.2017 / 18:17
0

Another solution is still to use the element button :

<button type="submit" name="plano" value="">Label</button>

When you set the type to submit and have the value attribute set, a key / value pair is generated in the information sent by the form when submitting it, just as with the input element. In this way, it would be possible to have the elements:

<button type="submit" name="plano" value="basico">Escolha o plano básico</button>
<button type="submit" name="plano" value="medio">Escolha o plano médio</button>
<button type="submit" name="plano" value="avancado">Escolha o plano avançado</button>

In PHP, to recover this value, you would only need to use $_POST or $_GET , depending on the method used by the form:

<?php

switch ($_POST["plano"]) {
    case "basico":
        echo "Você escolhe o plano básico", PHP_EOL;
        break;
    case "medio":
        echo "Você escolhe o plano médio", PHP_EOL;
        break;
    case "avancado":
        echo "Você escolhe o plano avançado", PHP_EOL;
        break;
}
  

See the W3C Element Specification for more details.

The main advantage of this method compared to the answer from Guilherme is that by using button you can set the same value of attribute name for all buttons, varying the value in value . So, in PHP, you just need to validate on a value and not on three, as is using input .

Even doing the above is not so good. Ideally, if the existing plans were stored in the database, associate the value of the button with the value of id of the plane and not with the value of the name. Something like:

<button type="submit" name="plano" value="1">Escolha o plano básico</button>
<button type="submit" name="plano" value="2">Escolha o plano médio</button>
<button type="submit" name="plano" value="3">Escolha o plano avançado</button>

And in PHP just do:

$plano = $_POST["plano"];

Being able to save the value in the database directly.

    
07.07.2017 / 20:35