[英]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.