sql - PHP: How to receive multiple values with ajax?

101

I have a pagepractice2.html in which a customer selects the product, color and quantity. After the user submits, the values are sent along with the product price and balance to another pageaddtocart.php via ajax.

Theaddtocart.php page receives those values, connects to the database and inserts those values. However,addtocart.php is not receiving those values and no values are visible in the alerts messages in the code below.

I think there is a problem in theaddtocart.php page on lineif(getenv('REQUEST_METHOD') == 'POST') but I do not know how to fix it:

    practice2.php
      function addtocart(price){
      var productdetails=document.forms['mehendicones'].value;
      var pcolor=document.getElementById('mehendi_color').value;
    var pquantity=document.getElementById('quantity_mehendi_color').value;
         var bal=price*pquantity;
             var ajax=new XMLHttpRequest();
              ajax.open("POST","addtocart.php",true);
                ajax.send(productdetails,pcoloR,price,pquantity,bal);
        ajax.onreadystatechange=function()
        {
        if(this.readyState==4 && this.status==200){
           alert(this.responseText);
                    }
                }
        }

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

           // I think there is an issue with the post call here
           if(getenv('REQUEST_METHOD') == 'POST'){
           $pdetails=file_get_contents("php://input");
            $pcolor=file_get_contents("php://input");
             $price=file_get_contents("php://input");
             $pquantity=file_get_contents("php://input");
              $pbal=file_get_contents("php://input");
            }
            $fname=$_SESSION['firstname'];
           $id=$_SESSION['id'];
            $nam=$_SESSION['code']; 

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

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

People are also looking for solutions to the problem: Using PHP Google Photos Api with Server-To-Server communication

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.