我目前正在为我们公司创建预订页面。我想要做的就是当用户点击立即预订按钮时,会弹出一个表单供他们完成。用户完成表单后,对访问该站点的所有人永久禁用保留按钮。我怎么能做到这一点?我不是很熟悉java脚本或php。任何帮助将不胜感激。
<div class="container">
<div class="row">
<div class="col-lg-12">
<table class="table table-striped">
<tr>
<td>Product</td>
<td><a href="#" class="btn btn-primary ">RESERVE NOW</a></td>
</tr>
</table>
<form>
First name:<br>
<input type="text" name="firstname" value="Mickey">
<br>
Last name:<br>
<input type="text" name="lastname" value="Mouse">
<br><br>
<input type="submit" value="Submit">
</form>
</div>
</div>
</div>
我嘲笑了一个可以在这里找到的例子:
https://jsfiddle.net/rman215/gw2m53w9/1/
答案 0 :(得分:0)
如果我理解你,你想提供能力保留一些东西,但仅限1人,对吗?
因此,您可以在数据库中存储标志(例如,在disabled_at
[timestamp,default = null]列中。当有人点击按钮并调用处理页面(通过ajax或简单重定向)时,设置标记disabled_at
值NOW()
。每次处理页面都是disabled_at IS NULL
。如果是 - 将名字和姓氏保存到数据库。
在包含表单的页面上,如果disabled
disabled_at IS NOT NULL
以提交按钮
修改强>
<强> disable.php 强>
<?php
function reserveItem($name = null, $value = null)
{
$name .= '.reserved';
$value = $value ? $value : 1;
return file_put_contents($name, $value);
}
function isDisabled($name = null)
{
return file_exists($name . '.reserved');
}
$disabled = isDisabled('item1');
if (isset($_POST['submit'])) {
$name = isset($_POST['firstname']) ? $_POST['firstname'] : '';
$lastName = isset($_POST['lastname']) ? $_POST['lastname'] : '';
if (!$disabled) {
reserveItem('item1', $name . $lastName);
}
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<div class="container">
<div class="row">
<div class="col-lg-12">
<table class="table table-striped">
<tr>
<td>Product</td>
<td><a href="#" class="btn btn-primary ">RESERVE NOW</a></td>
</tr>
</table>
<form method="post" action="disable.php">
First name:<br>
<input type="text" name="firstname" value="Mickey">
<br>
Last name:<br>
<input type="text" name="lastname" value="Mouse">
<br><br>
<input type="submit" value="Submit" <?= $disabled ? 'disabled' : ''; ?>>
</form>
</div>
</div>
</div>
</body>
</html>
注意:代码仅在编辑器中编写,未经过测试。
答案 1 :(得分:0)
给id提交按钮,点击提交调用这个功能
function disableButton() {
$("#idOfButton").attr("disabled","disabled")
}
答案 2 :(得分:0)
表单提交后,您将获得成功回复。获得成功后,只需使用以下代码禁用立即预订按钮:
如果您使用ajax提交表单,那么您需要这样做: 假设立即预订按钮 id:reserve_btn_id
$.ajax({
// your code....
}).done(function() {
$("#reserve_btn_id").attr("disabled","disabled");
});
答案 3 :(得分:0)
由于每次客户端加载页面时都会执行JavaScript
,因此您无法插入JavaScript
代码来阻止该按钮。
我建议你使用一个简单的MySQL
数据库,例如:
CREATE TABLE Items(
ItemID INTEGER AUTO_INCREMENT PRIMARY KEY,
Description VARCHAR(255),
IsReserved TINYINT(1) //this works like a boolean
);
这样您的页面就可以选择所有项目,如下所示:
SELECT * FROM Items ORDER BY ItemID
然后你会用这样的东西打印出来:
$server = "localhost";
$username = "root";
$password = "root";
$database = "mydatabase";
$connection = mysqli_connect($server, $username, $password, $database);
$result = mysqli_query($connection, "SELECT * FROM Items ORDER BY ItemID");
if (mysqli_num_rows($result) > 0){
echo("<form action=\"mypage.php\" method=\"post\">");
while($row = $result->fetch_assoc()){
echo("input name=\"id\" type=\"hidden\" value=" . $row['ItemID'] . ">"); //this input type hidden isn't shown on the page but we use it to store the id of the item so that we can retrieve it on the PHP page on submit
echo("ID: " . $row['ItemID']);
echo("Description: " . $row['Description']);
if ($row['IsReserved'] == 1){
echo("<button class=\"mybutton\" type=\"submit\" disabled>Reserve</button>");
}
else{
echo("<button class=\"mybutton\" type=\"submit\">Reserve</button>");
}
}
echo("</form>");
}
else{
echo("No data");
}
然后要更新已保留项目的状态,只需在表单提交到另一个PHP
页面时更新状态,您将得到以下内容:
$server = "localhost";
$username = "root";
$password = "root";
$database = "mydatabase";
$connection = mysqli_connect($server, $username, $password, $database);
$id = (int) $_POST['id']; // here you get the id of the hidden input
// $data = mysqli_query($connection, "SELECT * FROM Items WHERE ItemID = $id");
/* $data now contains all info stored about the item, you can use it if you
need to insert those info in another table, etc. */
// UPDATING STATUS
mysqli_query($connection, "UPDATE Items SET IsReserved = 1 WHERE ItemID = $id");
mysqli_close($connection);
所以就是这样,基本上,我希望它可以帮助你完成你的项目:)
我告诉你this guide如何处理Forms&amp;他们的数据为PHP
。