详细信息:嘿,伙计们,我到处寻找解决我的问题,但无济于事,请不要报告问题,直到你了解我的问题。我有一个索引页,其中包含sql,我拉添加产品到我的页面,并希望创建一个ajax请求与每个产品的id,这是在按钮中呼应,以创建一个产品详细信息页面,但我似乎不能得到产品id张贴在产品详细信息页面对正确方向的任何帮助将不胜感激。谢谢你!
精确错误:Notice: Undefined index: id inC:'xampp'htdocs'website'includes'product_detail.php on line 7
我尝试过的事情:将"post"更改为"post"添加数据类型这就是我看到的所有错误
Jquery代码:function detail(ProID){
var data = {"id" : ProID};
jQuery.ajax({
url: '/website/includes/product_detail.php',
method : "post",
data: data,
success : function(data){
window.open("includes/product_detail.php","_self");
},
error:function(){
alert("Something when wrong!");
}
});
}
</script>
HTML/php代码:
<?php
require_once 'dbconnect/dbconnect.php';
include 'includes/head.php';
include 'includes/header.php';
include 'includes/navigation.php';
include 'includes/footer.php';
$sql = "SELECT * FROM products WHERE featured = 1";
$featured = $db->query($sql);
?>
<div class="main-content">
<div class="main-body">
<h2 class="center-header">Featured Products</h2>
<?php while($product = mysqli_fetch_assoc($featured)): ?>
<div class="row_one">
<div class="product-one" >
<h4><?= $product['ProName']; ?></h4>
<a href="includes/product_detail.php">
<img class="product_img_one"src="<?= $product['Image']; ?>" alt="<?= $product['ProName']; ?>">
</a>
<p>List Price:<s class="list_price">$<?= $product['List_Price'];?></s></p>
<p>Sales Price:$<?= $product['Price'];?></p>
<button type="button" class="product-detail"
onclick= "detail(<?= $product["ProID"]; ?>)">Details</button>
</div>
<?php endwhile; ?>
</div>
</div>
</div>
</body>
</html>
请求应该工作的地方:
<?php
require_once '../dbconnect/dbconnect.php';
include 'head.php';
include 'header.php';
include "navigation.php";
include "footer.php";
$id = $_POST["ProID"];
$id = (int)$id;
$sql = "SElECT * FROM products WHERE ProID = '$id'";
$result = $db->query($sql);
$product = mysqli_fetch_assoc($result);
?>
应该是
$id = $_POST["id"]; not $id = $_POST["ProID"];
在product_detail.php页面
我怀疑在jquery ajax中使用method : 'POST'
的方法是否应该用大写字母;
// this is what i am using as template for sending request after organizing data in json format...
$.ajax({
method: "POST",
url: "submit.php",
data: {Product_Name: "theProduct_Name", Price: 500, "Product_ID": "theProduct_ID"},
success : function(responseText)
{
alert('success to reach backend');
// you can console the responseText and do what ever you want with responseText
console.log(responseText);
},
error : function(jqXHR, status, error)
{
if (jqXHR.status === 0)
{
alert('Not connected.'nPlease verify your network connection.');
}
else if (jqXHR.status == 404)
{
alert ('The requested page not found. [404]');
}
else if (jqXHR.status == 500)
{
alert ('Internal Server Error [500].');
}
else if (exception === 'parsererror')
{
alert ('Requested JSON parse failed.');
}
else if (exception === 'timeout')
{
alert ('Time out error.');
}
else if (exception === 'abort')
{
alert ('Ajax request aborted.');
}
else
{
alert ('Uncaught Error.'n' + jqXHR.responseText);
}
}
});