Disable button via php/jquery/javascript

Asked

Viewed 103 times

1

Greetings!
I have a page in which contains a 'search' field, when typing something, the field returns similar names.
Example: If I type "a" it returns me "amateur, lover, Amarildo, amoxilina, attendant, allocate." and etc.

And there is a button in index.php that serves to send such requests

The question is, if I type "-" in the field, it will not return me anything, because there is no such value in the database, however, I can still send the requests (which would only be to save the names that appear on a page), in this case, as there is no name similar to "-", it sends the request, filling a page field with a null value (blank).

I wanted to somehow prevent it being possible to send requests if there was no such value within the database, one way I thought was:

This is the button code, which allows me to send the request

<button> Enviar<i class="zmdi zmdi-arrow-right"></i></button>

If I can check the upload field, if it is incorrect, I could disable the button in such a way

<button disabled=""> Enviar<i class="zmdi zmdi-arrow-right"></i></button>

Is there any way to make this change to html code via php? or some better method to handle such an operation..?

Note: This is the only button on the page, so it wouldn’t be a specific button (because using a code specifying the change of the button status would certainly disable everyone on the page)

Edit1: "index.php" connects to the database and handles it by selecting it to the search field in such a way:

$query ="SELECT * FROM bancod WHERE id like '" . $_POST["keyword"] . "%' ORDER BY id LIMIT 0,9";
$result = mysqli_query($con, $query);
$count=  mysqli_num_rows($result);
    if($count < 1){
        echo "Esse valor não existe no banco de dados ";
    }

So it already checks the existence of the entered value, whether or not it contains in the database. If it does not exist, it prints me "This value does not exist in the database". However, I can still send the request by clicking on the button, but as I said, it fills the fields nullally, all blank ): and I just wanted to prevent it from being possible to send it

  • 1

    "I wanted to somehow prevent it being possible to send requests if there is no such value within the database"... how will you know that there is no value in the database without making a request?

  • @Sam Edited the topic, "Edit1:" answers the question

  • The only thing I can think of, from what I understand, is to disable the button if the input field is empty or create some criteria in the frontend. For example, check if the field contains only letters etc...

  • This would be a good one, I thought about it, but being more direct, the field will only have numerical searches, example: "20200200013" <= this value exists in the BD, if I type "20200200014", it prints me the message "This value does not exist in the database", but if I click "Send"(button) it sends, even if the given number does not exist in the BD and fills a table with all blank (null values)

  • If there is no BD, simply empty the input and disable the button.

3 answers

0

There must be a better solution, but it might work

In your query:

$query ="SELECT * FROM bancod WHERE id like '" . $_POST["keyword"] . "%' ORDER BY id LIMIT 0,9";
$result = mysqli_query($con, $query);
$count=  mysqli_num_rows($result);
if($count < 1){
    $btn_disabled = 'disabled';
}else $btn_disabled = '';

And on the button:

<button <?=$btn_disabled ?> > Enviar<i class="zmdi zmdi-arrow-right"></i></button>

0

I do it in many ways. You can do via PHP, if you already return the result of SELECT and realize that the value is correct, you can already put the disabled in the button

Example in PHP: Return the value in the variable created in php.

<?php if($valor != null or $valor != 0){
   $variavel_disabled = 'disabled';
}else{
  $variavel_disabled = '';
}?>

<button <?php echo $variavel_disabled ?> >Enviar<i class="zmdi zmdi-arrow-right"></i></button>

Or, if you send html via javascript and will return by it, you can also disable by javascript.

var btn = $('.zmdi');
btn.attr('disabled', true);

there are several ways to do, see which fits best.

0

The best way to do this would be really with Ajax, OO and maybe the use of PDO, but the way it is my suggestion is very similar to Lucas, only change the query.

$query ="SELECT COUNT(id) as total FROM bancod WHERE id like '" . $_POST["keyword"] . "%'";
$result = mysqli_query($con, $query);
if($result['total'] < 1){

That way the bank consumption would be lower and you would only bring d database information that you would use.

Browser other questions tagged

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