[英]Ajax Send ID To Another HTML Page With AJAX To Create Where Clause
更新時間:2017年5月17日上午9:50
我有兩個HTML頁面,都具有通過頁面加載獲取數據的AJAX方式。 但是,現在我想將id發送到另一個HTML頁面,以將帶where子句的數據提取到我的子類別頁面中,該id來自類別頁面。 我該怎么辦? 或者我錯過了或需要使用以下哪些代碼?
如何在兩個同時加載Ajax的html頁面之間創建where子句?
我在https://github.com/Ailyn09/AJAX-Fetch創建了一個副本
我使用以下代碼:
category.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="description" content="">
<meta name="author" content="">
<link rel="icon" href="../../favicon.ico">
<title></title>
<!-- Bootstrap core CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" integrity="sha384-rwoIResjU2yc3z8GV/NPeZWAv56rSmLldC3R/AZzGRnGxQQKnKkoFVhFQhNUwEyJ" crossorigin="anonymous">
<!--Font-awesome 4.7.0-->
<link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
<!-- Custom styles for this template -->
<link href="css/carousel.css" rel="stylesheet">
</head>
<div class="row" id="fetch_allcategory"></div>
<!-- /.container -->
<script src="https://code.jquery.com/jquery-3.1.1.slim.min.js" integrity="sha384-A7FZj7v+d/sdmMqp/nOQwliLvUsJfDHW+k9Omg/a/EheAdgtzNs3hpfag6Ed950n" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/tether/1.4.0/js/tether.min.js" integrity="sha384-DztdAPBWPRXSA/3eYEEUWrWCy7G5KFbe8fFjk5JAIxUYHKkDx6Qin1DkWx51bBrb" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/js/bootstrap.min.js" integrity="sha384-vBWWzlZJ8ea9aCX4pEW3rVHjgjt7zpkNpZk+02D9phzyeVkE+jo0ieGizqPLForn" crossorigin="anonymous"></script>
<script type="text/javascript" src="js/jquery-3.2.1.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$('#fetch_allcategory').load('php/category_fetch.php', function(response, status, xhr ){
});
$("#footer").load("footer.html");
});
</script>
</body>
category.php
<?php
$conn = new mysqli('localhost', 'root', '123', 'orderingsystem');
if ($conn->connect_error) {
die("Connection error: " . $conn->connect_error);
}
$result = $conn->query("SELECT * FROM categories ORDER BY category_name");
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {?>
<div class="col-lg-3 product-category">
<div class="card">
<img class="card-img-top" id="image" value="<?php echo $row['id']?>" src="images/category/<?php echo $row['categoryimage'] ?>" alt="Card image cap" onclick="window.location.href = 'subcategory.html'">
<div class="card-block">
<h4 class="card-title"><?php echo $row['category_name']?></h4>
<p class="card-text"><?php echo $row['description'] ?></p>
</div>
</div>
</div>
<?php }
}
?>
<script>
$(document).ready(function(){
$("#image").click(function(){
var id = $('#image').attr('value');
$.post("subcategory.php",{catid: id },function(d){
console.log(d);
});
});
});
</script>
subcategory.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="description" content="">
<meta name="author" content="">
<link rel="icon" href="../../favicon.ico">
<title></title>
<!-- Bootstrap core CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" integrity="sha384-rwoIResjU2yc3z8GV/NPeZWAv56rSmLldC3R/AZzGRnGxQQKnKkoFVhFQhNUwEyJ" crossorigin="anonymous">
<!--Font-awesome 4.7.0-->
<link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
<!-- Custom styles for this template -->
<link href="css/carousel.css" rel="stylesheet">
</head>
<div class="row" id="fetch_subcategory"></div>
<!-- /.container -->
<script src="https://code.jquery.com/jquery-3.1.1.slim.min.js" integrity="sha384-A7FZj7v+d/sdmMqp/nOQwliLvUsJfDHW+k9Omg/a/EheAdgtzNs3hpfag6Ed950n" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/tether/1.4.0/js/tether.min.js" integrity="sha384-DztdAPBWPRXSA/3eYEEUWrWCy7G5KFbe8fFjk5JAIxUYHKkDx6Qin1DkWx51bBrb" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/js/bootstrap.min.js" integrity="sha384-vBWWzlZJ8ea9aCX4pEW3rVHjgjt7zpkNpZk+02D9phzyeVkE+jo0ieGizqPLForn" crossorigin="anonymous"></script>
<script type="text/javascript" src="js/jquery-3.2.1.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$('#fetch_subcategory').load('subcategory_fetch.php')
});
</script>
</body>
</html>
subcategory.php
<?php
$conn = new mysqli('localhost', 'root', '123', 'orderingsystem');
if ($conn->connect_error) {
die("Connection error: " . $conn->connect_error);
}
if (isset($_POST['catid'])) {
$categoryid = $_POST['catid'];
$result = $conn->query("SELECT * FROM subcategories WHERE categoryid = '{$_POST['catid']}' ORDER BY name");
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {?>
<div class="col-lg-3 product-category">
<div class="card">
<img class="card-img-top" src="images/subcategory/<?php echo $row['subcategoryimages'] ?>" alt="Card image cap" onclick="window.location.href = 'products.html'">
<div class="card-block">
<h4 class="card-title"><?php echo $row['name'] ?></h4>
<p class="card-text"><?php echo $row['description'] ?></p>
</div>
</div>
</div>
<?php
}
}
}
?>
您可以使用它,也許效果很好。
<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
<script>
$( "#fetch_allcategory" ).load( "category_fetch.php", function( response, status, xhr ) {
});
</script>
轉到category_fetch.php
<?php
$conn = new mysqli('localhost', 'root', '123', 'orderingsystem');
if ($conn->connect_error) {
die("Connection error: " . $conn->connect_error);
}
$result = $conn->query("SELECT * FROM categories ORDER BY category_name");
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {?>
<div class="col-lg-3 product-category">
<div class="card">
<!---------- please check value for category id -------------->
<img class="card-img-top" id="image" value="<?php echo $row['id']?>" src="images/category/<?php echo $row['categoryimage'] ?>" alt="Card image cap" onclick="window.location.href = 'subcategory.html'">
<div class="card-block">
<h4 class="card-title"><?php echo $row['category_name']?></h4>
<p class="card-text"><?php echo $row['description'] ?></p>
</div>
</div>
</div>
<?php }
}
?>
<script>
$(document).ready(function(){
$("#image").click(function(){
var id = $('#image').attr('value');
$.post("subcategory.php",{catid: id },function(d){
console.log(d);
});
});
});
</script>
然后轉到subcategory.php文件
<?php
$conn = new mysqli('localhost', 'root', '123', 'orderingsystem');
if ($conn->connect_error) {
die("Connection error: " . $conn->connect_error);
}
$categoryid = $_POST['catid'];
$result = $conn->query("SELECT * FROM subcategories where category_id = {$categoryid} ORDER BY name");
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {?>
<div class="col-lg-3 product-category">
<div class="card">
<img class="card-img-top" src="images/subcategory/<?php echo $row['subcategoryimages'] ?>" alt="Card image cap" onclick="window.location.href = 'products.html'">
<div class="card-block">
<h4 class="card-title"><?php echo $row['name'] ?></h4>
<p class="card-text"><?php echo $row['description'] ?></p>
</div>
</div>
</div>
<?php }
}
?>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.