[英]Pass variable string from javascript to php using ajax
How to pass a variable value from html page using javascript to php? 如何使用javascript将HTML网页中的变量值传递给php?
i created this code in my index.php 我在index.php中创建了此代码
$amount = $_GET['pricenumb'];
echo $amount;
and this is my javascript code to call on click of button and send the data to the PHP file. 这是我的javascript代码,只需单击一下按钮即可调用并将数据发送到PHP文件。
<script type="text/javascript">
$(".cell").on("click", "input:checkbox", function () {
var thiss = $(this);
var total = $("#price");
var target = $("label[for='" + thiss.attr("id") + "']");
var item_value = +(target.html().replace(/[^0-9\.]/g, "") || 0);
var cur_total = +(total.html().replace("$", "") || 0);
if (thiss.prop("checked") === true) {
cur_total += item_value;
} else {
cur_total -= item_value;
};
total.text("$" + cur_total);
});
</script>
<script type="text/javascript">
$("#pay_btn").on("click", function () {
var price = $("#price").text();
var pricenumb = price.replace(/[^0-9\.]/g, "");
$.ajax({
type: "POST",
url: "forumdisplay.php?fid=2",
data: "price=" + price + "pricenumb="+ pricenumb,
cache:false,
success: function(){
}
});
});
</script>
and this is the checkbox, 这是复选框
<div class="cell">
<div class="form-check"><label for="check-a" class="form-check-label"><input id="check-a" class="form-check-input" type="checkbox">$166<span class="form-check-sign"></span></label>
<div class="mask visible-on-sidebar-regular">Buy Product</div>
</div>
</div>
the work code is, when I check the checkbox, it will update the div content, and I want when I click on pay button, get the div value via javascript and send the value to my index.php 工作代码是,当我选中该复选框时,它将更新div的内容,我想当我单击pay按钮时,通过javascript获取div的值并将该值发送到我的index.php
You are using POST
in your ajax and GET
in php, chage your ajax to GET
. 您在ajax中使用
POST
,在php中使用GET
,将ajax更改为GET
。 Also, In your ajax change 另外,在您的ajax更改中
type: "POST",
url: "forumdisplay.php?fid=2",
data: "price=" + price + "pricenumb="+ pricenumb,
to 至
type: "GET",
url: "forumdisplay.php",
data: {
price: price,
pricenumb: pricenumb,
fid: 2
}
That's not how you pass data in ajax. 那不是您在ajax中传递数据的方式。 The correct format is to use curly braces and define props name and then value
正确的格式是使用花括号并定义道具名称和值
data:{propName1: value1,propsName2: value2,propsName3: "Some string value"}
Which can be used in the file like this in case of POST request. 在POST请求的情况下,可以在这样的文件中使用它。
$_POST['propName1']
which will give value1 variable data as a result $_POST['propName1']
作为结果将提供value1变量数据
$_POST['propName3']
which will give output as Some string value
string $_POST['propName3']
将输出为Some string value
字符串
The value can be in quotes if it's a string or not in quotes if it's a variable. 如果是字符串,则该值可以用引号引起来;如果是变量,则可以不用引号引起来。 So you need to redefine your ajax data props to
因此,您需要将ajax数据道具重新定义为
$.ajax({
type: "POST",
url: "forumdisplay.php?fid=2",
data: {price: price ,pricenumb: pricenumb},
cache:false,
success: function(response){
// Things to do on success
},
error: function(error){
// Error handling in case of error
}
});
These values you passed can be used in the file forumdisplay.php
with $_POST['price']
and $_POST['pricenumb']
. 您传递的这些值可以在
forumdisplay.php
$_POST['price']
和$_POST['pricenumb']
。 The name inside the $_POST is the propsName inside data props in ajax function. $ _POST内的名称是ajax函数中数据属性内的propsName 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.