PHP - Get Select values

Asked

Viewed 45 times

0

Hello,

My project consists of... having a form with a select. That 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". OK...

I want to. Check the Select and if the value is... 1 it will automatically bsucar 15... If it is 2, will bsucar 25..

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

Code example:

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

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

I’d like to get a while, Foreach sap.. 'Cause I have to keep making the same code for all values... and a single code would save a lot.

  • It will be easier if you add the HTML code and describe what "automatically fetch" means.

  • I simply want him to check all 2,4,8,16 and 32 Select values, and then check what the request was

  • It is not very clear, you can give two examples, what happens if the value is 2 and 4?

  • ok.. If the value is 2, it asks for 3 points, and so on.

  • See if these answers can help you;insert link description here

1 answer

2

it was not very clear what you want, but I will try to make an example for both cases.

case 1:

First if you want to do something different for each value, the best way is to use swich and create a case for each 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 method in_array to check if there is any value in a previously declared value array.

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 that will surely save you future work is to use a constant to create select and valid data. This way the data is centralized in one place, when necessary to change something this 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>

Browser other questions tagged

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