sql - PHP: How to receive multiple values with ajax?
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)
?>