我想使用结果值并将其传递给php变量,这是我的代码…
billingCoffee.php
$("#linkAddSize").click(function(e){
e.preventDefault();
var txtCoffeeName = document.getElementById("txtCoffeeName").value;
var cmbSizes = document.getElementById("cmbSizes").value;
var txtPrice = document.getElementById("txtPrice").value;
$.ajax({
url: "addSizeandPrice.php",
type: "POST",
data: {coffeename: txtCoffeeName, sizes: cmbSizes, price: txtPrice},
datatype: "json",
success: function (result){
//set it php variable
}
});
});
addSizeandPrice.php
if($tableresult){
$query = "INSERT INTO tbl$CoffeeName (CoffeeSize, Price) VALUES ('$Size', '$Price');";
$insertresult = mysqli_query($con, $query);
if($insertresult){
SESSION_START();
$_SESSION['nameCoffee'] = $CoffeeName;
echo $_SESSION['nameCoffee'];
}
else{
echo "Something went wrong!";
}
}
我想在不刷新页面的情况下使用变量…我有这个想法使用AJAX,但不知道如何设置它在php变量。
您正在使用POST作为向PHP脚本发送变量的方法。在PHP中,它们位于超全局变量$_POST
$coffeename = $_POST['coffeename'];
进一步阅读:http://php.net/manual/en/reserved.variables.post.php