简体   繁体   中英

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,

 <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'],

thanks

Yes, this is possible. But you'll have to call the PHP-Script with POST or GET Parameters.

Edit: But I don't think that you can put the variable directly into the session-array.

For example, use $.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

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

Optionally, you can specify a function that receives the result of the asynchronous request as third parameter of $.get() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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