How do I show a person’s name and not their key in a script that uses onclick()?

Asked

Viewed 56 times

0

I’m having a question with the client exclusion code, instead of typing the client key to be deleted I believe one that returns all the stored customer names where I choose one and click delete and ready.

Only instead of showing Leandro foi excluído(a) com sucesso showcase 6 foi excluído(a) com sucesso, is showing the client key instead of the name (need to make this as accessible as possible because a visually impaired person will test so I’m making it more accessible to him when using Windows Narrator).

inserir a descrição da imagem aqui

The code is this

<!DOCTYPE html>
<html lang="pt-br">
<head>
    <meta charset="utf-8"> 
    <title> Remover cliente </title>
    <link rel="stylesheet" href="/WEB/css/css.css">
    <script>
        function exibirNome() {
            var nome = document.querySelector("#cdcliente").value;
            if (nome) {
                alert(nome + " foi excluído(a) com sucesso");
            }
        }
    </script>  
</head>
<body> 
    <?php
        require_once '../conexao/conexao.php';  
        if(isset($_POST['Deletar'])){
            $cd_cliente = $_POST['cd_cliente'];
            try {
                $remove = "DELETE FROM cliente WHERE cd_cliente = :cd_cliente";
                $remocao = $conexao->prepare($remove);
                $remocao->bindValue(':cd_cliente',$cd_cliente); 
                $remocao->execute();
            } catch (PDOException $falha_remocao) {
                echo "A remoção não foi feita".$falha_remocao->getMessage();
            }
            $seleciona_nomes = $conexao->query("SELECT cd_cliente, nome FROM cliente");
            $resultado_selecao = $seleciona_nomes->fetchAll();
        }   
    ?>
    <form method="POST">
        <p> ID cliente:
        <select name="cd_cliente" required="" id="cdcliente" title="Campo para digitar o código do cliente a ser excluído">
            <option value="" title="Escolha abaixo o cliente a ser excluído"> </option>
            <?php
                foreach ($resultado_selecao as $valor) {
                    echo "<option value='{$valor['cd_cliente']}'>{$valor['nome']}</option>";
                }
            ?>
        </select>
        <p> <input type="submit" name="Deletar" onclick="exibirNome()" title="Botão para confirmar a exclusão do cliente" value="Deletar cliente"> </p>
    </form>
  • Brother, see if this link helps you. https://answall.com/questions/253124/como-pega-o-texto-texto-op%C3%A7%C3%A3o-selected-within-a-select

  • Before asking a question, try to see if there is already a question in which the answer meets your need. This is one of the basic premises that people ask here in stackoverflow. But that’s it, you’re on the right track. Good luck!

  • 1

    complementing the Cat, this is a problem of javascript/html, your example has no error, just taking a property that does not satisfy you.

  • @Lennons.Bueno there is something I didn’t expect.

2 answers

3


In its function displayName()

function exibirNome() {
   var nome = document.querySelector("#cdcliente").value;
   if (nome) {
      alert(nome + " foi excluído(a) com sucesso");
   }
}

Change nome for this reason:

function exibirNome() {
   var select = document.querySelector("#cdcliente");
   var nome = select.options[select.selectedIndex].text;

   if (nome) {
      alert(nome + " foi excluído(a) com sucesso");
   }
}
  • Now I understand, thank you.

2

Instead of .value utilize .selectedOptions[0]. text

This will solve the problem as it looks for the text inside the tag option selected, not its value.

Browser other questions tagged

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