我正在使用HTML和SQLite进行清单,使用PHP连接到服务器并提取数据。提取数据后,我需要填充一个HTML表,但这不起作用。当我尝试执行console.log以查看其工作情况时,它不会在Chrome控制台中显示。 :(
请询问是否需要该数据库文件,因为它是工作文件夹中的本地数据库文件。
body {
font-family: 'Open Sans', sans-serif;
}
table, th, td {
border: 1px solid black;
border-collapse: collapse;
padding: 15px;
text-align: center;
}
table tr:nth-child(even) {
background-color: #eee;
}
table tr:nth-child(odd) {
background-color: #fff;
}
table th {
color: white;
background-color: black;
}
/* Style the tab */
.tab {
overflow: hidden;
border: 1px solid #ccc;
background-color: #f1f1f1;
}
/* Style the buttons that are used to open the tab content */
.tab button {
background-color: inherit;
float: left;
border: none;
outline: none;
cursor: pointer;
padding: 14px 16px;
transition: 0.3s;
}
/* Change background color of buttons on hover */
.tab button:hover {
background-color: #ddd;
}
/* Create an active/current tablink class */
.tab button.active {
background-color: #ccc;
}
/* Style the tab content */
.tabcontent {
display: none;
padding: 6px 12px;
border: 1px solid #ccc;
border-top: none;
animation: fadeEffect 1s;
}
/* Go from zero to full opacity */
@keyframes fadeEffect {
from {opacity: 0;}
to {opacity: 1;}
}
/* Style buttons */
.btn {
background-color: DodgerBlue; /* Blue background */
border: none; /* Remove borders */
color: white; /* White text */
padding: 12px 16px; /* Some padding */
font-size: 16px; /* Set a font size */
cursor: pointer; /* Mouse pointer on hover */
}
/* Darker background on mouse-over */
.btn:hover {
background-color: RoyalBlue;
}
.btnadd {
background-color: DodgerBlue; /* Blue background */
border: none; /* Remove borders */
color: white; /* White text */
padding: 12px 16px; /* Some padding */
font-size: 16px; /* Set a font size */
cursor: pointer; /* Mouse pointer on hover */
}
/* Darker background on mouse-over */
.btnadd:hover {
background-color: RoyalBlue;
}
/* The Modal (background) */
.modal {
display: none; /* Hidden by default */
position: fixed; /* Stay in place */
z-index: 1; /* Sit on top */
padding-top: 100px; /* Location of the box */
left: 0;
top: 0;
width: 100%; /* Full width */
height: 100%; /* Full height */
overflow: auto; /* Enable scroll if needed */
background-color: rgb(0,0,0); /* Fallback color */
background-color: rgba(0,0,0,0.4); /* Black w/ opacity */
}
/* Modal Content */
.modal-content {
position: relative;
background-color: #fefefe;
margin: auto;
padding: 0;
border: 1px solid #888;
width: 80%;
box-shadow: 0 4px 8px 0 rgba(0,0,0,0.2),0 6px 20px 0 rgba(0,0,0,0.19);
-webkit-animation-name: animatetop;
-webkit-animation-duration: 0.4s;
animation-name: animatetop;
animation-duration: 0.4s
}
/* Add Animation */
@-webkit-keyframes animatetop {
from {top:-300px; opacity:0}
to {top:0; opacity:1}
}
@keyframes animatetop {
from {top:-300px; opacity:0}
to {top:0; opacity:1}
}
/* The Close Button */
.close {
color: white;
float: right;
font-size: 28px;
font-weight: bold;
}
.close:hover,
.close:focus {
color: #000;
text-decoration: none;
cursor: pointer;
}
.modal-header {
padding: 2px 16px;
background-color: #5cb85c;
color: white;
}
.modal-body {padding: 2px 16px;}
<!DOCTYPE html>
<html>
<head>
<title>Inventory Server</title>
<link rel="stylesheet" a href="InventoryServer.css"/>
<link href="https://fonts.googleapis.com/css?family=Open+Sans" rel="stylesheet">
<link href="https://fonts.googleapis.com/icon?family=Material+Icons"
rel="stylesheet">
</head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<body>
<h2 style="text-align: center">Welcome to the inventory!</h2>
<!-- Trigger/Open The Modal -->
<button class="btnadd" id="btnadd"><i class=material-icons>add_circle</i></button>
<div class="tab">
<button class="tablinks" onclick="openLocation(event, 'Kitchen')" id="defaultOpen">Kitchen</button>
<button class="tablinks" onclick="openLocation(event, 'Shed')">Shed</button>
</div>
<div id="Kitchen" class="tabcontent">
<h3>Kitchen</h3>
<table style="width: 100%">
<tr>
<th>Name</th>
<th>Quantity</th>
<th>Unit</th>
<th>Description</th>
</tr>
<?php
$db = "inventory.db";
$handle = sqlite_open($db) or die("Could not open database");
$query = "SELECT Name,Quantity,Unit,Description FROM kitchen ";
$result = sqlite_query($handle, $query) or die("Error in query: ".sqlite_error_string(sqlite_last_error($handle)));
while($row = sqlite_fetch_array($result)) {
?>
<tr>
<td><?php echo $row['Name']?></td>
<td><?php echo $row['Quantity']?></td>
<td><?php echo $row['Unit']?></td>
<td><?php echo $row['Description']?></td>
</tr>
<?php
}
?>
<?php
sqlite_close($handle);
?>
</table>
</div>
<div id="Shed" class="tabcontent">
<h3>Shed</h3>
<table style="width: 100%">
<thead>
<tr>
<th>Name</th>
<th>Quantity</th>
<th>Unit</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<?php
$connect = mysql_connect("inventory.db","root", "", "inventory");
if (!$connect) {
die(mysql_error());
}
$results = mysql_query("SELECT * FROM shed ");
while($row = mysql_fetch_array($results)) {
?>
<tr>
<td><?php echo $row['name']?></td>
<td><?php echo $row['quantity']?></td>
<td><?php echo $row['unit']?></td>
<td><?php echo $row['description']?></td>
</tr>
<?php
}
?>
</tbody>
</table>
</div>
<!-- The Modal -->
<div id="btnAddForm" class="modal">
<!-- Modal content -->
<div class="modal-content">
<div class="modal-header">
<span class="close">×</span>
<h2 style="text-align: center">Add New Item To Inventory</h2>
</div>
<div class="modal-body">
<form action="#" method="post" role="form" data-toggle="validator" class="form-container">
<div class="form-group margin-top-m">
<label for="name" class="control-label">Name Of Item: </label>
<input type="text" id="nameOfItem" name="nameOfItem" class="form-control" data-error="Please enter the item name!" required>
<div class="help-block with-errors"></div>
</div>
<div class="form-group">
<label for="quantity" class="control-label">Quantity of Item: </label>
<input type="text" id="quantityOfItem" name="quantityOfItem" class="form-control required" data-error="Please enter the quantity of the item!" required>
<div class="help-block with-errors"></div>
</div>
<div class="row">
<div class="col-md-6 mid-col">
<div class="form-group">
<label for="unitOfItem" class="control-label">Item Unit: </label>
<select name="unitOfItem" id="unitOfItem" class="form-control required" data-error="Please select a unit!" required>
<option value=""></option>
<option value="piece">piece(s)</option>
<option value="grams">g</option>
<option value="kg">kg</option>
<option value="mL">mL</option>
<option value="L">L</option>
<option value="item">item(s)</option>
</select>
<div class="help-block with-errors"></div>
</div>
</div>
<div class="col-md-6 mid-col">
<div class="form-group">
<label for="place" class="control-label">Select a place: </label>
<select name="place" id="place" class="form-control required" data-error="Please select the item location!" required>
<option value=""></option>
<option value="Kitchen">Kitchen</option>
<option value="Shed">Shed</option>
</select>
<div class="help-block with-errors"></div>
</div>
</div>
</div>
<div class="form-group">
<label for="descriptionOfItem" class="control-label">Item description: </label>
<input type="text" id="descriptionOfItem" name="descriptionOfItem" class="form-control required" data-error="Please enter a valid item description!" required>
<div class="help-block with-errors"></div>
</div>
<p class="margin-0"><button type="submit" class="btn btn-lg orange w-100 margin-top-s">Submit</button></p>
</form>
</div>
</div>
</div>
<script type="text/javascript" src="require.js"></script>
<script>
// Get the modal
var modal = document.getElementById('btnAddForm');
// Get the button that opens the modal
var btn = document.getElementById("btnadd");
// Get the <span> element that closes the modal
var span = document.getElementsByClassName("close")[0];
// When the user clicks the button, open the modal
btn.onclick = function() {
modal.style.display = "block";
}
// When the user clicks on <span> (x), close the modal
span.onclick = function() {
modal.style.display = "none";
}
// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event) {
if (event.target == modal) {
modal.style.display = "none";
}
}
document.getElementById("defaultOpen").click();
function openLocation(evt, locationName) {
// Declare all variables
var i, tabcontent, tablinks;
// Get all elements with class="tabcontent" and hide them
tabcontent = document.getElementsByClassName("tabcontent");
for (i = 0; i < tabcontent.length; i++) {
tabcontent[i].style.display = "none";
}
// Get all elements with class="tablinks" and remove the class "active"
tablinks = document.getElementsByClassName("tablinks");
for (i = 0; i < tablinks.length; i++) {
tablinks[i].className = tablinks[i].className.replace(" active", "");
}
// Show the current tab, and add an "active" class to the button that opened the tab
document.getElementById(locationName).style.display = "block";
evt.currentTarget.className += " active";
}
</script>
</body>
</html>