我想访问PHP中的javascript变量。我该怎么做呢?
下面是我的按钮的javascript onClick,我在警报中得到了值。
$(".check").click(function(){
var priceee = document.getElementById("total-price").value;
//alert(priceee);
});
发布于 2016-09-14 18:05:45
Try below to send the JS data to PHP via AJAX,
$.ajax({
type: 'POST',
url: 'yourphppage.php',
data: {
'totalprice' : $("#total-price").val(),
},
success: function(response){
}
})
In yourphppage.php,
echo $_POST['totalprice'];
发布于 2016-09-14 18:21:10
您可以使用AJAX调用来执行此操作,可以使用phpuser所说的POST或GET方法。为了让它正常工作,您必须在服务器上运行它,所以要么在本地计算机(localhost)上使用类似XAMPP的东西,要么在实际的服务器上运行它。
这里有一个关于如何编写它的示例。
$(function () {
$(".check").click(function(){
var priceee = $("#total-price").val();
});
$.ajax({
type: 'POST',
url: 'file.php', //your php page
data: {
price: pricee
},
success: function (response) {
//the code you want to execute once the response from the php is successful
},
error: function () {
//error handling (optional)
}
});
});
您的PHP页面(在本例中为file.php)
<?php
if (isset($_POST['price'])) {
$price = $_POST['price'];
//now your variable is set. as $price in php
echo $price; //returns price as response back to jQuery
}
希望这对您有所帮助,请参阅jQuery Ajax调用文档(http://api.jquery.com/jQuery.ajax/)中的更多信息
Spalqui
https://stackoverflow.com/questions/39487581
复制相似问题