简体   繁体   English

通过按钮单击禁用/启用按钮

[英]Disabling/Enabling a button through button click

I am displaying SQL data in rows and each row contains "Add To Zip" Button.我在行中显示 SQL 数据,每行包含“添加到 Zip”按钮。 The button for each row is enabled.启用每一行的按钮。 I want to disable the current row's button on click.我想在点击时禁用当前行的按钮。 I later want to re-enable these disabled buttons through another button in a different php file.我稍后想通过不同 php 文件中的另一个按钮重新启用这些禁用的按钮。 How do I achieve this?我如何实现这一目标? Also, please do not point to SQL injection, I have used login details to check if the correct credentials are added or not and I will be only posting the code that I think is relevant to this question.另外,请不要指向 SQL 注入,我已使用登录详细信息检查是否添加了正确的凭据,我只会发布我认为与此问题相关的代码。

FetchData.php FetchData.php

...
if(mysqli_num_rows($result) > 0)
{
 $output .= '<h4 align = "center">Search Result</h4>';
 $output .= '<div class="table-responsive">
                <table class = "table table bordered">
                   <tr>
                        <th>ID</th>                                 
                    </tr>';
                    
    while($row = mysqli_fetch_array($result))
    {
                
        $output .= '
                <tr>
                    <td>'.$row["ID"].'</td>
                    <td><button type="button" class="btn btn-primary" onclick="addToPdf(\''.$row["ID"].'\', this)"><i class="fa fa-pdf" aria-hidden="true"> </i>Add to Zip</button></td>
                </tr>
            ';
    }
    echo $output;
    
}
...

AddToZip.php AddToZip.php

...
...
$sql = "INSERT INTO Zip (ID)
VALUES ('$id')";

if ($conn->query($sql) === TRUE) {
    echo "New record created successfully";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}

$conn->close();

header("Refresh: 0");
...
...

EDIT: I am using AJAX to display the fetched values from FetchData.php编辑:我正在使用 AJAX 显示从FetchData.php获取的值

Index.php索引.php

<html>
 <head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>LiveData</title>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
  <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" />
 </head>
 <body>
  <div class="container">
   <br />
   <h2 align="center">Live Data Search</h2><br />
   <div class="form-group">
    <div class="input-group">
     <span class="input-group-addon">Search</span>
     <input type="text" name="search_text" id="search_text" placeholder="Search by ID" class="form-control" />
    </div>
    
   </div>

   <br />
   <div id="result"></div>
  </div>
 </body>
</html>


<script>
$(document).ready(function(){
    $('#search_text').keyup(function(){
        var txt = $(this).val();
        if(txt != '')
        {
            $.ajax({
                url:"FetchData.php",
                method: "post",
                data: {search:txt},
                dataType: "text",
                success: function(data)
                {
                    $('#result').html(data);
                }
            });
        }
       });

});

    var addToPdf = function(id, btn) {
    $.get({
        url:"AddToZip.php?id=" + id,
        success: function(data) {
            btn.disabled = true;
            
        }
    });
  };

</script>

...
...

There are a couple of issues with your HTML markup.您的 HTML 标记存在几个问题。 Ie you have a <button> tag inside an anchor <a> tag which is technically incorrect.即,您在锚<a>标记内有一个<button>标记,这在技术上是不正确的。 However, you most likely have some Bootstrap styling on the button tag and are only using the anchor to action the AddToZip.php call right?但是,您很可能在按钮标签上有一些引导样式,并且仅使用锚点来执行 AddToZip.php 调用,对吗?

If that's the case, I would rather strip out the <a> tag and use an onclick method on the <button> .如果是这种情况,我宁愿去掉<a>标签并在<button>上使用 onclick 方法。 An example:一个例子:

Replace代替

<td><a href="AddToZip.php?id='.$row["ID"].'" target="target"><button  id="pdf" name="AddToZip" action="" class="btn btn-primary"><i class="fa fa-pdf"" aria-hidden="true"> </i>Add to Zip</button></a></td>
<iframe style="display:none;" name="target"></iframe>

With

<td><button id="pdf" name="AddToZip" type="button" class="btn btn-primary" onclick="document.getElementById(\'frame-x\').src=\'AddToZip.php?id='.$row["ID"].'\'; this.disabled = true;"><i class="fa fa-pdf" aria-hidden="true"> </i>Add to Zip</button></td>
<iframe style="display:none;" name="target" id="frame-x"></iframe>

To be clear要清楚

This is not an ideal solution, just a quick fix to achieve what the OP asked for help with.这不是一个理想的解决方案,只是实现 OP 寻求帮助的快速解决方案。 To the OP, I highly recommend using an Ajax call of sorts to communicate to the back-end.对于 OP,我强烈建议使用 Ajax 调用来与后端通信。

I suggest looking into jQuery's AJAX API我建议查看jQuery 的 AJAX API


Update after OP updated Question OP更新后更新问题

So now that I see you are using jQuery AJAX calls, add a function to your script section:所以现在我看到你正在使用 jQuery AJAX 调用,添加一个 function 到你的脚本部分:

<script>

$(document).ready(function(){
    ...
};


var addToPdf = function(id, btn) {
    $.get({
        url:"AddToZip.php?id=" + id,
        success: function(data) {
            btn.disabled = true;
        }
    });
};
</script>

Then change your markup to this (remove the iframe as well):然后将您的标记更改为此(同时删除 iframe):

<td><button type="button" class="btn btn-primary" onclick="addToPdf(\''.$row["ID"].'\', this)"><i class="fa fa-pdf" aria-hidden="true"> </i>Add to Zip</button></td>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM