为什么我的Jquery Ajax请求无法正常运行错误:未定义的索引:ProID?

时间:2016-09-27 02:59:17

标签: javascript php jquery ajax

详细信息:嘿伙计们我搜遍了我的问题的解决方案,但无济于事,请不要报告问题直到你理解我的问题。我有一个索引页面,其中包含sql,我将其添加到我的页面,并希望创建一个带有每个产品ID的ajax请求,该按钮在按钮中回显以创建产品详细信息页面但是我似乎无法获得产品要在产品详细信息页面上发布任何帮助,我们将不胜感激。谢谢!

  

确切错误:注意:未定义的索引:id in   第7行的C:\ xampp \ htdocs \ website \ includes \ product_detail.php

我尝试过的事情: 将“发布”更改为“发布” 添加数据类型 这就是我看到的所有错误

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);
?>

2 个答案:

答案 0 :(得分:0)

应该是

 $id = $_POST["id"]; not  $id = $_POST["ProID"];

在product_detail.php页面上

答案 1 :(得分:0)

我怀疑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);
                    }
                 }
      });