本文介绍了使用Ajax将变量字符串从javascript传递到php的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用javascript将html页面中的变量值传递给php?

How to pass a variable value from html page using javascript to php?

我在index.php中创建了这段代码

i created this code in my index.php

$amount = $_GET['pricenumb'];
echo $amount;

这是我的javascript代码,只需单击按钮即可调用并将数据发送到PHP文件.

and this is my javascript code to call on click of button and send the data to the PHP file.

<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>

这是复选框,

<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>

工作代码是,当我选中复选框时,它将更新div的内容,我想要当我单击付款"按钮时,通过javascript获取div的值并将该值发送到我的index.php

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

推荐答案

那不是您在ajax中传递数据的方式.正确的格式是使用大括号并定义道具名称,然后定义值

That's not how you pass data in ajax. The correct format is to use curly braces and define props name and then value

数据:{propName1:value1,propsName2:value2,propsName3:某些字符串值"}

在POST请求的情况下,可以在这样的文件中使用哪个

Which can be used in the file like this in case of POST request.

  • $ _ POST ['propName1'] 作为结果将提供value1变量数据

  • $_POST['propName1'] which will give value1 variable data as a result

$ _ POST ['propName3'] ,它将以某些字符串值字符串

如果该值是字符串,则该值可以用引号引起来;如果是变量,则该值不能用引号引起来.因此,您需要将ajax数据道具重新定义为

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({
        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
        }
});

您传递的这些值可以在文件 forumdisplay.php 中与 $ _ POST ['price'] $ _ POST ['pricenumb'] . $ _ POST 中的名称是Ajax函数中数据道具中的 propsName .

These values you passed can be used in the file forumdisplay.php with $_POST['price'] and $_POST['pricenumb']. The name inside the $_POST is the propsName inside data props in ajax function.

这篇关于使用Ajax将变量字符串从javascript传递到php的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 23:11