为什么我的PHP页面没有收到任何响应并且没有其他错误?

时间:2019-04-08 11:25:48

标签: php jquery json ajax

我有2页,Practice.php和addtocart.php。在Practice.php中,我从表单收集信息,然后使用AJAX将信息发送到addtocart.php。然后,此页面连接到数据库并插入我的值。但是,我没有从addtocart.php收到任何响应,并且数据库中也没有插入任何数据。

我一直在试图找出问题所在,但似乎找不到。请帮忙。

practice.php

   //form from which addtocart is being called
     <form id="mehendicones">
      Color: <input list="mehendi_color" class="mehendi_color" 
         id="mehendi_color1">
                <datalist id="mehendi_color">
                    <option value="Black">
                    <option value="White">
                    <option value="Brown">
                    <option value="Dark Red"> 
                </datalist>
                    <br>
                <p id="price">17</p>
        Quantity<input type="number" min="5" max="15" 
             id="quantity_mehendi_color" step="1">

        <input type="submit" value="ADD TO CART" style="text-align:center;" 
           onclick="addtocart()" >

            </form>

function addtocart() {
  var request = new XMLHttpRequest();
  var url = "addtocart.php";
  request.open("POST", url, true);
  request.setRequestHeader("Content-Type", "application/x-www-form- 
      urlencoded");
  request.onreadystatechange = function () {
    if (request.readyState === 4 && request.status === 200) {
      alert(request.response);
    }
  };
  var pdetails = "mehendi cones";
  var pquantity = document.forms["mehendicones"] 
  ["quantity_mehendi_color"].value;
  var pcolor = document.forms["mehendicones"] 
  ["mehendi_color1"].value;
  var price = document.getElementById("price").innerHTML;

  var data = ({
    "pdetails": pdetails, 
    "pquantity": pquantity, 
    "pcolor": pcolor, 
    "price": price
  });

  request.send(data);
}

addtocart.php

<?php
  include 'includes/database.php';

  // Handling data in JSON format on the server-side using PHP
        header("Content-Type: application/json");
        // build a PHP variable from JSON sent using POST 
                     //method
        if($_POST){
$v= json_decode(stripslashes(file_get_contents("php://input")));
        echo json_encode($v);
        $v->pdetails;
        $v->pcolor;
        $v->pquantity;
        $v->price;
               $fname=$_SESSION['firstname'];
        $id=$_SESSION['id'];
                  $nam=$fname.$id;

             $sql="INSERT INTO ".$nam." 
         (userid,pdetails,price,pquantity,pcolor) VALUES 
             ('$id','$pdetails','$price','$pquantity','$pcolor');";

    $result=mysqli_query($con,$sql);
        if(mysqli_num_rows($result) > 0){
            echo "added to cart";
        }else{
            echo "couldnot add to cart..try again";
        }
    }

            mysqli_close($con);


              ?>

0 个答案:

没有答案