简体   繁体   中英

How to access PHP variable inside JavaScript object array

I am trying to access PHP variable inside JavaScript function, I am using PayPal create button API and trying to send amount which is store in PHP variable into JavaScript function which is object array.

The problem is that I tried different ways to access PHP variable inside JavaScript but none of them working.

Here is the part of code:

PHP Part:

    <?php
          //I tested this amount is working fine
          $myamount = $order['amount'];
          $dec = '.00';
          $AMT = $myamount . ' ' . $dec; 

    ?>

JS Part:

 paypal.Buttons(
    {

         createOrder: function(data, actions) {
         // Set up the transaction

         return actions.order.create({
           purchase_units: [{
             amount: {

                   value: amt
             }
           }]
         });
       }
     }

 ).render('#paypal-button-container');

On browser console I am getting this "INVALID PARAMETER SYNTAX", "description": "The value of a field does not conform to the expected format.

Updated code, now in a single block. I did spot a mistake in your PHP code which would cause errors, that was the space you had in between the amount and the ".00". This would cause JS variables not to work correctly since you would be storing the value, for example of "123 .00" as opposed to "123.00".

PHP Code

$AMT = $order['amount'].'.00'; 

Javascript Code:

paypal.Buttons() <-- The closing bracket was missing.
{
    createOrder: function(data, actions) {

        // Set up the transaction
        return actions.order.create({
            purchase_units: [{
                amount: {
                    value: '<?=$AMT;?>' // Short code for the print or echo function.
                }
            }]
        });
    }
}.render('#paypal-button-container');

Try with Storing value in hidden field and access

<input type="hidden" id="xyz" value="<? echo $AMT ?>" />



 createOrder: function(data, actions) {
  // Set up the transaction
   var amt = document.getElementById('xyz').value;
   return actions.order.create({
           purchase_units: [{
             amount: {

                   value: amt
             }
           }]
         });
       }
     }

I'm first going to guess that the PHP code and the JS code are contained within the same PHP file, if not, then it is never going to work, not without using session variables or as such.

If they are in the same file, you will need to go into the source code of the web page containing the JS function to see what value has been placed within the $AMT substitution.

If you do and place the contents of that within the OP, people should be able to help you better.

I've refactored your code and spotted an issue:

paypal.Buttons() <-- The closing bracket was missing.
{
    createOrder: function(data, actions) {

        // Set up the transaction
        return actions.order.create({
            purchase_units: [{
                amount: {
                    value: '<?=$AMT;?>' //unable to retrieve value
                }
            }]
        });
    }
}.render('#paypal-button-container');

In your original code, the function "paypal.Buttons" did not have a closing bracket which is used for passing parameters into the function.

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