0

I need to assign javascript variable in to php variable in the javascript function This is my current code

<script>
$(document).ready(function() {
    $('#datashow').on('click', '#proceed', function(event) {
        event.preventDefault();
        $('#confirmView').hide();
        $('#confirmView').show('slow');
        var price = (document.getElementById("amount").value);
        var pass = 'Htj3Q3Yp10718900415738';
        var key = '<?php echo md5(microtime() . rand()); ?>';
        var pass = pass.concat(key);
        if (price != '') {
            $("#amountLabel").text(price);
            $("#PurchaseAmt").text(price);
             var pass = pass.concat(price);
             var pass = pass.concat('144');
             <?php $enc = '<script> pass </script>'?>
             var enc = '<?php echo  base64_encode(pack('H*', sha1($enc)));?>'
             $("#pass").text(enc);

        }
        else {
            $("#amountLabel").text('0');
        }

    });
});

My question is How to assign this var pass in to $enc?please help me

  • you can assign php variable to javascript, but not javascript variable to php, as php is executed server side and javascript is executed client side... although you can use `ajax`... – Nishant Solanki Apr 29 '15 at 06:02
  • What you're asking is **impossible**. PHP executes first, sends the data to the browser, then the browser executes the javascript. As above, you'll need to send the data back to the server via ajax, or figure out a different way to solve your problem. The question of passing javascript variables to php has been asked **thousands** of times before, did you try searching first? Remember to search *before* you ask a question! – Christian Apr 29 '15 at 06:06
  • how to pass this var enc = '' pass instead of $enc..do you know any idea about it? – krishantha Apr 29 '15 at 06:07
  • Thanks Christian varga.i searched earlier but not found any answer.that is why i posted this.if you get inconvenience im sorry. – krishantha Apr 29 '15 at 06:13
  • For preparing enc var try to use javascript base 64 encoding instead of trying base64_encode function from PHP. you don't need to write PHP code there. – Ganesh Patil Apr 29 '15 at 06:15
  • Hi ganesh do you know javascript 64 encoding function?please give me reference for that – krishantha Apr 29 '15 at 06:21
  • Check this one of it works for you .https://scotch.io/quick-tips/how-to-encode-and-decode-strings-with-base64-in-javascript – Ganesh Patil Apr 29 '15 at 06:32

0 Answers0