PHP - Get Select values

0

Hello,

My project consists of ... having a form with a select. This select has:

                <form action='/paypoints.php' method='post' id='coin_form'>
                    <select name='payer_amount' class='form-control' required>
                        <option value=''>- Escolher Valor -</option>
                        <option value='2'>3 Pontos - 2 moedas</option>
                        <option value='4'>6 Pontos - 4 moedas</option>
                        <option value='8'>12 Pontos - 8 moedas</option>
                        <option value='16'>24 Pontos - 16 moedas</option>
                        <option value='32'>50 Pontos - 32 moedas</option>
                    </select> 
                    <input type='submit' name='paycoin' class='btn btn-primary-filled btn-left btn-newsletter btn-rounded inside' value='Trocar Moedas'/>
            </form>

My plan is to check through Foreach or Other ... The requested value, example "15". Okay ...

I want is .. That check the Select and if the value is ... 1 it automatically goes bsucar 15 ... If it is 2, will bsucar 25 ..

Is there any way to accomplish this through Foreach or others ...

Sample code:

//Se valor for 2
if($ads_selected == 2) {

    //realiza algo... para o 2.                        
} 

I would like to get a While, Foreach seila .. Well I will always have to do the same code for all values ... and a single code would save a lot.

    
asked by anonymous 19.04.2018 / 02:59

1 answer

2

It's not very clear what you want, but I'll try to set an example for both.

Case 1:

Firstly if you want to do something different for each value, the best way is to use swich and create a case for every possible answer.

// paypoints.php
$payerAmount = $_POST['payer_amount'];

switch ($payerAmount) {
    case 2:
        // faz alguma coisa ...
        break;
    case 4:
        // faz alguma coisa ...
        break;
}

Case 2:

If the values are treated the same can be done using an implementation using the in_array method. to check if there is any value in a previously declared array of values.

Example 1:

// paypoints.php
$values = array(2, 4, 8, 16, 32);
$payerAmount = $_POST['payer_amount'];

// verifica se o valor $payerAmount está contino no array $values
if (in_array($payerAmount, $values)) {
    // faz alguma coisa ...
}

Another better alternative that requires a little more effort in the first implementation will certainly save you future work is to use a constant to create the select and validate the data. This way the data is centralized in one place, when it is necessary to change something it will be done only in one place.

Example 2:

// paypoints.php
const VALUES = array(
    2 => '3 Pontos - 2 moedas',
    4 => '6 Pontos - 4 moedas',
    8 => '12 Pontos - 8 moedas',
    16 => '24 Pontos - 16 moedas',
    32 => '50 Pontos - 32 moedas'
);

// verifica se a variável 'payer_amount' está vindo do post
// essa verificação é necessário pois quando a variável não está
// setada o php enviara uma mensagem 'notice'
if (isset($_POST['payer_amount'])) {
    $payerAmount = $_POST['payer_amount'];

    // verifica se a variável $payerAmount(2,4,8...) é uma key da
    // constante VALUES que foi préviamente declarada.
    if (array_key_exists($payerAmount, VALUES)) {
        // faz alguma coisa
    }
}

For this second implementation it is also necessary to change the file containing your html.

// formulario.phtml
<?php include ('paypoints.php'); ?>
<form action='paypoints.php' method='post' id='coin_form'>
    <select name='payer_amount' class='form-control' required>
        <option value=''>- Escolher Valor -</option>
        // percorre o constante VALUES (declarada no arquivo paypoints.php
        // botando as chaves da constante (2,4,8..) como value das options
        // e o valor (descrição) como texto de dentro da option 
        <?php foreach (VALUES as $key => $value) { ?>
            <option value="<?php echo $key; ?>"><?php echo $value; ?></option>
        <?php } ?>
    </select> 
    <input type='submit' name='paycoin' class='btn btn-primary-filled btn-left btn-newsletter btn-rounded inside' value='Trocar Moedas'/>
</form>
    
20.04.2018 / 16:37