简体   繁体   English

如何从另一个页面更改按钮的状态?

[英]How can I change the state of a button from another page?

I'm working on a site that has some strict specifications. 我正在一个有一些严格规格的网站上工作。 I'm interested to know if what I'm trying to achieve is possible and if yes, how. 我想知道我想要实现的目标是否可能,如果可以,如何实现。

I have a page displaying a list of products and a button next to each one called Reserve . 我有一个页面,显示一个产品列表,每个产品旁边都有一个按钮,称为“ Reserve I get the information from a MySQL database. 我从MySQL数据库获取信息。 The code is something like this (it's not the actual code): 代码是这样的(不是实际的代码):

while($obj = $results->fetch_object()) {
    echo '<div class="product">'; 
    echo '<iframe name="votar" style="display:none;"></iframe>';
    echo '<form method="post" action="reserve.php" target="votar">';
    echo $obj->description.' <button class="reserve" >Reserve</button>';
    echo '<input type="hidden" name="barcode" value="'.$obj->bar_code.'"/>';
    echo '</form>';
    echo '</div>';
}

Notice the iframe there. 注意那里的iframe I use this because when the button Reserve is clicked, the page is not supposed to be refreshed. 我用这个,因为按钮时Reserve被点击,页面应该被刷新。

The reserve.php page contains the following: reserve.php页面包含以下内容:

if (isset($_POST["barcode"])) {
    $barcode = filter_var($_POST["barcode"], FILTER_SANITIZE_STRING);

    $results = $mysqli->query("UPDATE devices 
                              SET status='reserved' 
                              WHERE bar_code=".$barcode);
}

Now, the actual question that I have is: Is it possible to click that Reserve button, not refresh the page, but still change the button to the disabled state? 现在,我的实际问题是:是否可以单击该“ Reserve按钮,而不刷新页面,但仍将按钮更改为禁用状态? I figured that the disabling action should be done from the reserve.php hence the question title. 我认为应该从reserve.php中执行禁用操作,从而解决问题标题。

If not, please advise me. 如果没有,请告诉我。 What I'm trying to achieve is, when a user scrolls down the product page and clicks Reserve , I want the button to become disabled. 我想要实现的是,当用户向下滚动产品页面并单击Reserve ,我希望该按钮被禁用。 I wouldn't mind the refresh per se, but what I mind is that when the refresh happens, the user is sent back to the beginning of the page and has to scroll down again if he wants to reserve another product. 我不介意刷新本身,但是我介意的是,发生刷新时,用户将被发送回页面的开头,并且如果他要保留其他产品,则必须再次向下滚动。

Try ajax ... it is the possible solution to update the value in db without refreshing the page 尝试ajax ...这是可能的解决方案,无需刷新页面即可更新db中的值

<input type='button' name='reserve' onclick='return doReserve(your_primary_key)' value='reserve' />

Now consider below javascript function to fire an ajax call 现在考虑下面的javascript函数来触发ajax调用

function doReserve(id)
    var data= {id: id};
            $.ajax({
                url: "ajax_reserve.php",
                data: data,
                type: "GET",
                success: function(html) {
                     if(html=='ok')
                    {
                         $('#reserve').prop('disabled', true);
                    }
                    else
                    {
                      alert('There was some error')
                    }
                }
            });
         }

and now create a php file ajax_reserve.php 现在创建一个PHP文件ajax_reserve.php

and put your database update code there in that file 并将数据库更新代码放在该文件中

$update= 'your update check variable i.e true or false';
if($update)
{
    echo 'ok';
}
else
{
    echo 'error';
}

let me know if any further help needed 让我知道是否需要进一步的帮助

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

相关问题 react native:当我从另一个页面单击按钮时,如何更改视图? - react native: How can I change a view when i click a button from another page? 如何从另一个页面更改页面的html代码? - How can I change html code of a page from another page? 如何更改另一个组件的状态? - How can I change the state of another component? 如何更改按钮状态直到按下另一个按钮? - How do I change a button state until another button is pressed? 如何使用 Javascript 在页面加载期间更改按钮的切换 state? - How can I change the toggle state of a button during page load with Javascript? 我如何从一个页面制作一个按钮,以自动触发另一个页面的特定按钮? - How i can make a button ,from a page , to automatically trigger a specific button from another page? 当我单击另一个组件上的按钮时如何更改组件状态 - How to change a component state when i click on a button that is on another component 如何更改另一个按钮 onPress onReact Native 的 state? - How do i change the state of another button onPress onReact Native? 当 go 到另一个页面时,如何将数据表 state 保留到另一个页面? - How can I keep the datatable state when go to another page? 如何在React Native页面中安排状态更改? - How can I orchestrate state change in my React Native page?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM