Set the name of the select input from the database

Asked

Viewed 29 times

0

How do I make the item description (coming from the bank) appear in select instead of the phrase "Select Equity Account" I want it to appear: $listar3["DES_CONTA_PATRIMONIAL"] for the selected item.

<select class="form-control" name="contaPatrimonial" onchange="this.form.submit();" <?php if(!isset($_GET["revenda"]) ) { ?> hidden  <?php } ?>  >
                       <option value="" disabled selected>Selecione a Conta patrimonial</option>

                       <?php
                            $linha3 = $stmt3->fetchAll(PDO::FETCH_ASSOC);
                            foreach($linha3 as $listar3){
                        ?>
                        <option value="<?php echo $listar3["CONTA_PATRIMONIAL"]; ?>"> <?php echo utf8_encode($listar3["DES_CONTA_PATRIMONIAL"]);?> </option>

                        <?php } ?>    
                </select>

Bench

if ( isset($_GET["revenda"]) ) {
    $var1 = $_GET["revenda"];
    $query = ("SELECT REVENDA, CONTA_PATRIMONIAL, DES_CONTA_PATRIMONIAL FROM AFX_CONTA_PATRIMONIAL WHERE REVENDA like :rev "); 
    $stmt3 = $pdo->prepare($query);
    $stmt3->bindValue(':rev', '%' . $var1 . '%', PDO::PARAM_STR);
    $stmt3->execute();
}

1 answer

1


One of the many possible ways.

<select class="form-control" name="contaPatrimonial" id="contaPatrimonial" onchange="this.form.submit();" <?php if(!isset($_GET["revenda"]) ) { ?> hidden  <?php } ?>  >
    <option disabled selected value="">Selecione algo</option>
    <?php
    $linha3 = $stmt3->fetchAll(PDO::FETCH_ASSOC);

    foreach($linha3 as $listar3){
        ?>
        <option value="<?php echo $listar3["CONTA_PATRIMONIAL"]; ?>"> <?php echo utf8_encode($listar3["DES_CONTA_PATRIMONIAL"]);?> </option>

    <?php } ?>    
</select>

<script>
    $("#contaPatrimonial").val("<?php echo isset($_GET["contaPatrimonial"]) ? $_GET["contaPatrimonial"] : ''?>");
</script>
  • It did not roll.. select had several repetitions of results and is already coming with a preset database item ..

  • Oops! Something was missing.

  • i would like to keep the phrase "Select the account." and after the user selects the page will update and select stay with the selected result

  • I already added what was missing, sorry for the glitch!

  • almost worked.. now select ta always getting the first result

  • all options are equal?

  • no.. are showing the right results.. but when the page updates select is selected in the first item and not in which I selected

Show 3 more comments

Browser other questions tagged

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