Show Alert on select

Asked

Viewed 944 times

1

I have the following code

$query = mysql_query("SELECT id, nome, situacao FROM clientes ORDER BY nome") or print mysql_error()

<select class="js-example-basic-single form-control" id="cliente" name="cliente">
   <option value="Selecione o Cliente"> Selecione o Cliente</option>
        <?php                           
          while($dados = mysql_fetch_array($query)) { ?>
           <option value="<?php echo $dados['id'] ?>">
        <?php 
        if ($dados['situacao']==1) {
          $dados['situacao']='Em dia';
        } else {
          $dados['situacao']='Devendo';
        }
          echo $dados['nome'] . ' -> '. $dados['situacao']; 
        ?>
        </option>
        <?php } ?>
    </select>

I needed that with the same field check situacao, showed an Alert showing that the customer is in debt

1 answer

1


You can check whether in the string inside your option which has been selected has the word Devendo, it shows the Alert.

Using jQuery is an option:

$('select').on('change', function(){ 
  if( $(this).find('option:selected').text().indexOf('Devendo') > 0) {
    alert('O Cliente esta devendo!');
  }
});

If you don’t want to use jQuery, below is only in Javascript:

var element = document.getElementById('cliente');

element.addEventListener("change", myFunction);

function myFunction() {
    var curSelected = this.querySelectorAll('option:checked')[0];
    if(curSelected && curSelected.innerHTML.indexOf('Devendo') > 0){
        alert ("O Cliente está devendo!");
    }
}

You can check working as below:

/* utilizando jQuery */
$('select').on('change', function(){ 
  if( $(this).find('option:selected').text().indexOf('Devendo') > 0) {
    alert('O Cliente esta devendo pelo jQuery!');
  }
});



/* utilizando apenas Javascript */
var element = document.getElementById('cliente');

element.addEventListener("change", myFunction);

function myFunction() {
    var curSelected = this.querySelectorAll('option:checked')[0];
    if(curSelected && curSelected.innerHTML.indexOf('Devendo') > 0){
    	alert ("O Cliente está devendo pelo Javascript!");
    }
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select class="js-example-basic-single form-control" id="cliente" name="cliente">
   <option value="Selecione o Cliente"> Selecione o Cliente</option>
   <option value="Selecione o Cliente2"> Selecione o Cliente 2 Devendo</option>
   <option value="Selecione o Cliente3"> Selecione Devendoo Cliente</option>
   <option value="Selecione o Cliente4"> Selecione o Cliente</option>
</select>

  • The OP commented that he was using jQuery? If not, it’s foolhardy to assume that he would include the library just for the sake of a alert()...

  • This is an option ... he also said that he could not use jQuery... qq way to do the same way using only Javascript...

  • I tried here it didn’t work

  • @wribeiro posta o resultado final do seu html... por favor - sem o PHP

  • 1

    Instead of giving a downvote in response, it is better to ask for OP improve the question. Why for a alert just do: <script type="text/javascript"> var situacao = "<?= $dados['situacao'] ?>"; if(situacao == "Devendo"){ alert("Devendo!");} </script>

  • People using jquery got, but gave another problem, Alert shows 3 or 4 times in a row, as if you had in the loop too

  • It’s actually in the loop... do you need Alert when you load? Or when you click on the options you have?

  • @andrepaulo it should show the Alert, when I click on the client should, and currently it is happening this, if you click on a client ok, it does not show the Alert, in the should occurs 2,3x does not have a certain number

  • You came to do as I proposed in the reply?

  • @andrepaulo used the following $('select'). on('change', Function(){ if( $(this). find('option:Selected'). text(). indexof('Should') > 0) { Alert('The Customer is owed by jQuery! '); } });

  • You put this at the bottom of your page?

  • @andrepaulo, exact created a function, and in select I call her with onchange

  • I think I have identified here, what happens is the following, when selecting the first time the client 'should' it shows 1x the Alert, if I click again on another it adds +1, then it shows 2x and so on, always adding 1

  • There’s the point... you don’t need to create function and you don’t need to call on the onchange.. This is already being done... just put the code at the bottom of the page... it will do just that for you.. I’ll explain better in the answer..

  • ah, got it right now

  • good! I’m glad the answer helped you

Show 11 more comments

Browser other questions tagged

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