简体   繁体   English

是否可以将变量的值从js文件发送到php文件

[英]Is it possible to send a value of a variable from a js file to a php file

I have script and I'd like to know if it's possible the send the value of a variable to $_session, 我有脚本,我想知道是否可以将变量的值发送到$ _session,

 <script>

$(function() {

    var delivery = 0;
    $('#jcart-paypal-checkout').click(function() {  
        delivery = $('form#delivery2 input[type=radio]:checked').val();
        if(!delivery) {
            alert('Please choose a delivery option');
            return false;
        }else {
            <?php $_SESSION['shipping'] = ?> delivery;
        }


    });

});
</script>

I need to send the value of delivery in $_session['shipping'], 我需要在$ _session ['shipping']中发送交货价值,

thanks 谢谢

Yes, this is possible. 是的,这是可能的。 But you'll have to call the PHP-Script with POST or GET Parameters. 但是,您必须使用POST或GET参数调用PHP脚本。

Edit: But I don't think that you can put the variable directly into the session-array. 编辑:但是我不认为您可以将变量直接放入会话数组。

For example, use $.get() : 例如,使用$.get()

$(function() {
    var delivery = 0;
    $('#jcart-paypal-checkout').click(function() {      
        delivery = $('form#delivery2 input[type=radio]:checked').val();
        if(!delivery) {
                alert('Please choose a delivery option');
                return false;
        }
        $.get('putInSession.php', { d: delivery });
    });
 });

This snippet expects a script called "putInSession.php" on the server side, which should check the input parameter "d" and then 此代码段在服务器端需要一个名为“ putInSession.php”的脚本,该脚本应检查输入参数“ d”,然后

$_SESSION["shipping"] = $_GET["d"];

Optionally, you can specify a function that receives the result of the asynchronous request as third parameter of $.get() . (可选)您可以指定一个函数,该函数接收异步请求的结果作为$.get()第三个参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM