Search in the database when loading the page

Asked

Viewed 45 times

-2

When loading the page I want you to make a query in the bank and check that the result brought more than 3 results, if you bring I want to block a checkbox. How can I do this? I’m using Jquery, PHP, Mysql.

  • $('[type="checkbox"]').attr('disabled', true), when I get the return: $('[type="checkbox"]').removeAttr('disabled').

1 answer

1


You can do this in many ways, it will depend on what your page looks like. For example, you can simply query the top of the page before rendering any HTML and check the condition, if it is true when adding the checkbox you place the attribute disabled.

Another way would be to make an ajax that returns the amount of records or even a boolean value and block the input depending on the result.

Using ajax:

$.get('consulta.php', function(registros) {
  if (registros > 3) {
    $("#checkbox").attr("disabled", true);
  }
});

Using PHP right at the top of the page:

//não sei como você está acessado o banco ... 
$iQtdRegistros = query();
$attr = $iQtdRegistros > 3 ? "disabled='disabled'" : '';


<input type="checkbok" <?php echo $attr; ?> >
  • Thanks Jorge, the text was much better.

Browser other questions tagged

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