我制作了一个ajax代码来清空ajax函数中的一个类getFailOutput()
但我坚持使用ajax函数getSuccessOutput()
来了解如何显示来自css类myAjax.php
的产品{ {1}}。
当我点击测试成功链接时,我无法从myajax.php显示该类..请帮助!!
products-wrp
myAjax.php
<?php
session_start(); //start session
include("config.inc.php"); //include config file
?>
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Stores</title>
<link href="style/style1.css" rel="stylesheet" type="text/css">
<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
</head>
<body>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a href="#" onclick="return getSuccessOutput();"> test success </a>
| <a href="#" onclick="return getFailOutput(); return false;"> test failure</a>
<div id="output">waiting for action</div>
<div align="left">
<script>
function getFailOutput() {
$.ajax({
success: function () {
$('.products-wrp').html('');
},
});
return false;
}</script>
<script>
function getSuccessOutput() {
$.ajax({
url:'myAjax.php',
success: function (response) {
console.log(data, response);
$('.products-wrp').html('');
$('.products-wrp').html(response);
},
});
return false;
}</script>
<?php
//List products from database
$results = $mysqli_conn->query("SELECT product_name, product_desc, product_code, product_image, product_price FROM products_list");
$products_list = '<ul id ="products_list" class="products-wrp">';
while($row = $results->fetch_assoc()) {
$products_list .= <<<EOT
<li>
<form class="form-item">
<h4>{$row["product_name"]}</h4>
<div>
<img src="images/{$row["product_image"]}" height="62" width="62">
</div>
<div>Price : {$currency} {$row["product_price"]}<div></form>
</li>
EOT;
}
$products_list .= '</ul></div>';
echo $products_list;
?>
</body>
</html>
答案 0 :(得分:0)
$('.products-wrp').html('');
$('.products-wrp').hide();
$('.products-wrp').html(response);
$('.products-wrp').show();
试试这个
答案 1 :(得分:0)
您可以使用jQuery load()
方法从特定部分中的网址加载数据。
例如:
$("#div").load("page_load.html #sectionToLoad");
在您的情况下,它可能是:
function getSuccessOutput() {
$('.products-wrp').load("myAjax.php .products-wrp")
return false;
}
有关进一步研究和示例,请转到此处 https://www.w3schools.com/jquery/jquery_ajax_load.asp