我在php中遇到POST问题

时间:2016-09-05 16:18:02

标签: javascript php html ajax post

嘿我仍然是php的初学者,我正在做一个详细模型,我想从POST获取id但我得到这个问题注意:未定义索引:ID在E:\ xampp \ htdocs \ Site \ includes \ detailmodal中。 php在第3行

这是功能:

       <script>
   function details(id)
   {
     var data = { "id" : id };
     jQuery.ajax({
     url : <?= BASEURL; ?>+'includes/detailmodal.php',
     method : 'POST',
     data : data,
     success : function(data){
      $('body').append(data);
      $('#details-modal').modal('toggle');
      },
      error : function(){
      alert("something went wrong")
      }
     });
     }
    </script>
    <script src="js/jquery-3.1.0.min.js" type="text/javascript"></script>
    <script src="js/bootstrap.js" type="text/javascript"></script>
    </body>
    </html>

这是index.php:

    <?php include_once 'core/init.php' ?>
    <?php include 'includes/head.php' ?>
    <?php include 'includes/navigation.php' ?>
    <?php include 'includes/header.php' ?>
    <?php include 'includes/detailmodal.php' ?>
    <?php include 'includes/footer.php' ?>
    <?php
     $sql='SELECT * FROM products';
     $featured=$db->query($sql);
     ?>
     <!-- Body -->

     <!-- Products -->
     <div class="container-fluid">
      <div class="col-md-2">
        left side
       </div>
     <div class="col-md-8">
     <div class="row">
       <h1 class="text-center" style="color:blue; font-family: 'Orbitron', sans-              serif; font-size:24px;">Products</h1><br>
     <?php while ($products = mysqli_fetch_assoc($featured)): ?>
      <div class="col-md-3">
       <h1 class="text-center" style="color:blue; font-family: 'Orbitron', sans-serif; font-size:16px;"><?= $products["title"]?></h1>
       <center> <img src="<?= $products["image"]?>" class="image-thumb"  alt="Procuct1"/></center>
       <p class="list-price text-danger text-center">List Price : <s><?= $products["list_price"]?></s> </p>
       <p class="price text-center">Our Price : <?= $products["price"]?> </p>
      <center> <button type="button" class="btn btn-sm btn-success" name="submit" onclick="details(<?php echo $products['id'];?>)">Details</button>      </center>
     </div>
     <?php endwhile;?>
    </div>
    </div>
    <div class="col-md-2">
    right side
  </div>
</div>
<!-- Modal -->

这是detailmodal.php:

    <?php
 require_once $_SERVER['DOCUMENT_ROOT'].'/site/core/init.php';
  $id = $_POST["id"];
  $id = (int)$id;
 $sql = "SELECT * FROM products WHERE id = '$id'";
 $result = $db->query($sql);
 $products = mysqli_fetch_assoc($result);
?>
<?php ob_start(); ?>
<div class="modal fade details-1" id="details-modal" tabindex="-1" role="dialog" aria-labelledby="details-1" aria-hidden="true">
  <div class="modal-dialog modal-lg">
    <div class="modal-content">
    <div class="modal-header">
      <button type="button" class="close" name="button" data-dismiss="modal" aria-label="Close">
         <span aria-hidden="true">&times;</span>
      </button>
      <h4 class="modal-title text-center" style="color:blue; font-family: 'Orbitron', sans-serif; font-size:40px;"><?php echo $products['title'];  ?></h4>
      <div class="modal-body">
        <div class="container-fluid">
          <div class="row">
            <div class="col-sm-6">
              <div class="center-block">
                <img src="<?php echo $products['image'];?>" alt="product1" class="details img-responsive"/>
              </div>
            </div>
            <div class="col-sm-6">
              <div class="center-block">
                <h4 class="text-center" style="color:White; font-family: 'Orbitron', sans-serif; font-size:25px;">Details</h4><br><br>
                <p style="color:black; font-family: 'Orbitron', sans-serif; font-size:25px;">
                      <?php echo $products['description'];?>
                </p>
                <form action="add-to-cart.php" method="post">
                  <div class="form-group">
                    <div class="col-xs-3">
                    <label id="quantitylbl" for="quantity">Quantity</label>
                      <input type="text" name="quantity" class="form-control" id="quantity" value="1" required>
                    </div>
                    <br>
                    <p>
                      available : 3
                  </div>

                    <div class="form-group">

                      <label id="sizelbl" for="size">Size</label>
                      <select name="size" id="size" class="form-control">
                        <option value="20">20</option>
                        <option value="22">22</option>
                        <option value="25">25</option>
                      </select>
                      </div>


                </form>
              </div>
            </div>
          </div>
        </div>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal" name="button">Close</button>
        <button type="submit" class="btn btn-warning"  name="button"><span class="glyphicon glyphicon-shopping-cart"></span>Add to Cart</button>
      </div>
    </div>
  </div>
</div>
</div>
<?php echo ob_get_clean(); ?>

2 个答案:

答案 0 :(得分:0)

ajax请求中的

更改行

method : "GET"

method : "POST"

同样在你的php文件中,在require

之后添加这一行
if($_SERVER['REQUEST_METHOD'] == 'POST'){

并在代码末尾关闭它

答案 1 :(得分:0)

将JQuery中的方法更改为POST

JQuery.ajax({
[...]
method: 'POST'
[...]