Doubt in <option> of <select>

Asked

Viewed 42 times

1

I’m trying to use the variable SESSION to pass values to fields, as was used in "login", where if there is something inside the variable, it will be shown in the field, but if it does not exist, nothing will be shown. How can I do the same in select?

<div class="form-group">
    <label for="nome" class="col-sm-2 control-label">Login</label>
    <div class="col-sm-10">
        <input type="text" class="form-control" name="login" placeholder="Login" value="<?php echo $_SESSION['login_consultar']; unset($_SESSION['login_consultar']);?>">
    </div>
</div>
<div class="form-group">
    <label for="nivel_acesso" class="col-sm-2 control-label">Nível de Acesso</label>
    <div class="col-sm-2">
        <select class="form-control" name="nivel_acesso">
            <option value="1">Administração</option>
            <option value="2">Informática</option>
        </select>
    </div>
</div>
  • And as the friend @Felipeduarte answered. It is to do so, but it is not used this way to do it. You must generate this content and pass the value with a variable.

  • And how do I do that? I started using the language recently.

  • How are you defining this $_session? Where the value comes from?

  • I’m using it this way: $_SESSION['nivel_acesso_consultar'] = $result['nivel_acesso']; - Onde $resultado é uma mysqli_fetch_assoc of a mysql query

  • Why are you moving to a Session? Why not go straight? Ta in another file the query?

  • Yes, it’s in another file.

  • 1

    Make a include in the middle of that file. Before displaying this form, you do the include.

  • 1

    Exercise a lot in language. Whenever you see a new function and do not know it, you learn about it. After that start programming on top of Frameworks. It will become easier.

  • I’ll do it. Thank you!

Show 4 more comments

2 answers

1

Thus

<?php if(isset($_SESSION['teste'])) : ?>
    <option value="<?= $_SESSION['teste'] ?>"> teste </option>
<?php endif; ?>

But if you’re using sessions only to pass values referring to the content, it is better to review its logic.

  • What do you recommend, then? I started working with the language a little while ago.

  • 1

    To pass values through the pages use $_POST or $_GET, through a form, if you need to recover data,between the pages, already enters the database issue, the superglobal $_SESSION is for example session control check whether a user was set or not, and it must be destroyed.

1


In this case you need to check the value of the variable for each option, and if it is the option select, print the attribute selected in option. Example:

<div class="form-group">
    <label for="nome" class="col-sm-2 control-label">Login</label>
    <div class="col-sm-10">
        <input type="text" class="form-control" name="login" placeholder="Login" value="<?php echo $_SESSION['login_consultar']; unset($_SESSION['login_consultar']);?>">
    </div>
</div>
<div class="form-group">
    <label for="nivel_acesso" class="col-sm-2 control-label">Nível de Acesso</label>
    <div class="col-sm-2">
        <select class="form-control" name="nivel_acesso">
            <option value="1"<?php if($_SESSION['login_consultar'] == '1') { echo ' selected="selected"'; } ?>>Administração</option>
            <option value="2"<?php if($_SESSION['login_consultar'] == '2') { echo ' selected="selected"'; } ?>>Informática</option>
        </select>
    </div>
</div>

Browser other questions tagged

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