title: How to dynamically change the price of paypal smartbutton according to the PHP cart value?
body: <p>I am not able to assign the value of my shopping cart to the Paypal button in order for the user to be able to pay the correct amount.</p>
  <p>The total price of the cart is stored as a variable <code>$total_price</code>:</p>
  <pre><code>&lt;span class=&quot;metadata&quot; id=&quot;totalprice&quot; value=&quot;&lt;?php echo $total_price ?&gt;&quot; &gt; &lt;/span&gt;
  </code></pre>
  <p>This is done to change it to something callable in Javascript.</p>
  <p>Paypal's code from their developer website is as such:</p>
  
  <pre class="lang-javascript prettyprint-override"><code>&lt;script&gt;
      var num = document.getElementById(&quot;totalprice&quot;).value;
      // Render the PayPal button into #paypal-button-container
      paypal.Buttons({
  
          // Set up the transaction
          createOrder: function (data, actions) {
              return actions.order.create({
                  purchase_units: [{
                      amount: {
                          value: num
                      }
                  }]
              });
          },
  
          // Finalize the transaction
          onApprove: function (data, actions) {
              return actions.order.capture().then(function (details) {
                  // Show a success message to the buyer
                  alert('Transaction completed by ' + details.payer.name.given_name + '!');
              });
          }
  
      }).render('#paypal-button-container');
  &lt;/script&gt;
  </code></pre>
  <p>However by adding my variable as the <code>value</code> (as shown in the code above) every time I run the payment, the Paypal pop up briefly opens and then crashes.</p>
  <p>I have also tried to create a Javascript variable containing the PHP variable and then setting <code>value</code> to that, but it had no effect.</p>
  <p>Is there any way to solve this?</p>
  <p>EDIT (this is my PHP code)</p>
  <pre class="lang-javascript prettyprint-override"><code>$cart_total=0;
  
  if(isset($_POST['submit'])){
      $address=obtain_values($connection,$_POST['address']);
      $city=obtain_values($connection,$_POST['city']);
      $pincode=obtain_values($connection,$_POST['pincode']);
      $payment_type=obtain_values($connection,$_POST['payment_type']);
      $user_id=$_SESSION['USER_ID'];
      foreach($_SESSION['cart'] as $key=&gt;$val){
          $productArr=get_product($connection,'','',$key);
          $price=$productArr[0]['price'];
          $quantity=$val['qty'];
          $cart_total=$cart_total+($price*$quantity);
          
      }
      $total_price=floatval($cart_total);
      $payment_status='pending';
      if($payment_type=='cod'){
          $payment_status='success';
      }
  $order_status='2';
  $added_on=date('Y-m-d h:i:s');
  
  $txnid = substr(hash('', mt_rand() . microtime()), 0, 20);
      
  mysqli_query($connection,&quot;INSERT INTO order(user_id,address,city,pincode,payment_type,total_price,payment_status,order_status,added_on,txnid) VALUES('$user_id','$address','$city','$pincode','$payment_type','$total_price','$payment_status','$order_status','$added_on','$txnid')&quot;);
  
  $order_id=mysqli_insert_id($connection);
  
  foreach($_SESSION['cart'] as $key=&gt;$val){
      $productArr=get_product($connection,'','',$key);
      $price=$productArr[0]['price'];
      $quantity=$val['qty'];
      
      mysqli_query($connection,&quot;insert into `order_detail`(order_id,product_id,qty,price) values('$order_id','$key','$quantity','$price')&quot;);
      echo &quot;insert&quot;;
  }
  unset($_SESSION['cart']);
  
  
      ?&gt;
      &lt;script&gt;
          window.location.href='thank_you.php';
      &lt;/script&gt;
      &lt;?php
  }   
  
  
  ?&gt;
  </code></pre>
  <p>EDIT 2</p>
  <pre class="lang-javascript prettyprint-override"><code>    &lt;span class=&quot;metadata&quot; id=&quot;totalprice&quot; value=&quot;&lt;?php echo ($total_price) ?&gt;&quot; &gt; &lt;/span&gt;
  &lt;div class=&quot;ht__bradcaump__area&quot; style=&quot;background: rgba(0, 0, 0, 0) url(images/bg/ali1.png) no-repeat scroll center center / cover ;&quot;&gt;
              &lt;div class=&quot;ht__bradcaump__wrap&quot;&gt;
                  &lt;div class=&quot;container&quot;&gt;
                      &lt;div class=&quot;row&quot;&gt;
                          &lt;div class=&quot;col-xs-12&quot;&gt;
                              &lt;div class=&quot;bradcaump__inner&quot;&gt;
                                  &lt;nav class=&quot;bradcaump-inner&quot;&gt;
                                    &lt;a class=&quot;breadcrumb-item&quot; href=&quot;index.html&quot;&gt;Home&lt;/a&gt;
                                    &lt;span class=&quot;brd-separetor&quot;&gt;&lt;i class=&quot;zmdi zmdi-chevron-right&quot;&gt;&lt;/i&gt;&lt;/span&gt;
                                    &lt;span class=&quot;breadcrumb-item active&quot;&gt;checkout&lt;/span&gt;
                                  &lt;/nav&gt;
                              &lt;/div&gt;
                          &lt;/div&gt;
                      &lt;/div&gt;
                  &lt;/div&gt;
              &lt;/div&gt;
          &lt;/div&gt;
          &lt;!-- End Bradcaump area --&gt;
          &lt;!-- cart-main-area start --&gt;
          &lt;div class=&quot;checkout-wrap ptb--100&quot;&gt;
              &lt;div class=&quot;container&quot;&gt;
                  &lt;div class=&quot;row&quot;&gt;
                      &lt;div class=&quot;col-md-8&quot;&gt;
                          &lt;div class=&quot;checkout__inner&quot;&gt;
                              &lt;div class=&quot;accordion-list&quot;&gt;
                                  &lt;div class=&quot;accordion&quot;&gt;
                                      
                                      &lt;?php 
                                      $accordion_class='accordion__title';
                                      if(!isset($_SESSION['USER_LOGIN'])){
                                      $accordion_class='accordion__hide';
                                      ?&gt;
                                      &lt;div class=&quot;accordion__title&quot;&gt;
                                          Checkout Method
                                      &lt;/div&gt;
                                      &lt;div class=&quot;accordion__body&quot;&gt;
                                          &lt;div class=&quot;accordion__body__form&quot;&gt;
                                              &lt;div class=&quot;row&quot;&gt;
                                                  &lt;div class=&quot;col-md-6&quot;&gt;
                                                      &lt;div class=&quot;checkout-method__login&quot;&gt;
                                                          &lt;form id=&quot;login-form&quot; method=&quot;post&quot;&gt;
                                                              &lt;h5 class=&quot;checkout-method__title&quot;&gt;Login&lt;/h5&gt;
                                                              &lt;div class=&quot;single-input&quot;&gt;
                                                                  &lt;input type=&quot;text&quot; name=&quot;login_email&quot; id=&quot;login_email&quot; placeholder=&quot;Your Email*&quot; style=&quot;width:100%&quot;&gt;
                                                                  &lt;span class=&quot;field_error&quot; id=&quot;login_email_error&quot;&gt;&lt;/span&gt;
                                                              &lt;/div&gt;
                                                              
                                                              &lt;div class=&quot;single-input&quot;&gt;
                                                                  &lt;input type=&quot;password&quot; name=&quot;login_password&quot; id=&quot;login_password&quot; placeholder=&quot;Your Password*&quot; style=&quot;width:100%&quot;&gt;
                                                                  &lt;span class=&quot;field_error&quot; id=&quot;login_password_error&quot;&gt;&lt;/span&gt;
                                                              &lt;/div&gt;
                                                              
                                                              &lt;p class=&quot;require&quot;&gt;* Required fields&lt;/p&gt;
                                                              &lt;div class=&quot;dark-btn&quot;&gt;
                                                                  &lt;button type=&quot;button&quot; class=&quot;fv-btn&quot; onclick=&quot;user_login()&quot;&gt;Login&lt;/button&gt;
                                                              &lt;/div&gt;
                                                              &lt;div class=&quot;form-output login_msg&quot;&gt;
                                                                  &lt;p class=&quot;form-messege field_error&quot;&gt;&lt;/p&gt;
                                                              &lt;/div&gt;
                                                          &lt;/form&gt;
                                                      &lt;/div&gt;
                                                  &lt;/div&gt;
                                                  &lt;div class=&quot;col-md-6&quot;&gt;
                                                      &lt;div class=&quot;checkout-method__login&quot;&gt;
                                                          &lt;form action=&quot;#&quot;&gt;
                                                              &lt;h5 class=&quot;checkout-method__title&quot;&gt;Register&lt;/h5&gt;
                                                              &lt;div class=&quot;single-input&quot;&gt;
                                                                  &lt;input type=&quot;text&quot; name=&quot;name&quot; id=&quot;name&quot; placeholder=&quot;Your Name*&quot; style=&quot;width:100%&quot;&gt;
                                                                  &lt;span class=&quot;field_error&quot; id=&quot;name_error&quot;&gt;&lt;/span&gt;
                                                              &lt;/div&gt;
                                                              &lt;div class=&quot;single-input&quot;&gt;
                                                                  &lt;input type=&quot;text&quot; name=&quot;email&quot; id=&quot;email&quot; placeholder=&quot;Your Email*&quot; style=&quot;width:100%&quot;&gt;
                                                                  &lt;span class=&quot;field_error&quot; id=&quot;email_error&quot;&gt;&lt;/span&gt;
                                                              &lt;/div&gt;
                                                              
                                                              &lt;div class=&quot;single-input&quot;&gt;
                                                                  &lt;input type=&quot;text&quot; name=&quot;mobile&quot; id=&quot;mobile&quot; placeholder=&quot;Your Mobile*&quot; style=&quot;width:100%&quot;&gt;
                                                                  &lt;span class=&quot;field_error&quot; id=&quot;mobile_error&quot;&gt;&lt;/span&gt;
                                                              &lt;/div&gt;
                                                              &lt;div class=&quot;single-input&quot;&gt;
                                                                  &lt;input type=&quot;password&quot; name=&quot;password&quot; id=&quot;password&quot; placeholder=&quot;Your Password*&quot; style=&quot;width:100%&quot;&gt;
                                                                  &lt;span class=&quot;field_error&quot; id=&quot;password_error&quot;&gt;&lt;/span&gt;
                                                              &lt;/div&gt;
                                                              &lt;div class=&quot;dark-btn&quot;&gt;
                                                                  &lt;button type=&quot;button&quot; class=&quot;fv-btn&quot; onclick=&quot;user_register()&quot;&gt;Register&lt;/button&gt;
                                                              &lt;/div&gt;
                                                          &lt;/form&gt;
                                                      &lt;/div&gt;
                                                  &lt;/div&gt;
                                              &lt;/div&gt;
                                          &lt;/div&gt;
                                      &lt;/div&gt;
                                      &lt;?php } ?&gt;
                                      &lt;div class=&quot;&lt;?php echo $accordion_class?&gt;&quot;&gt;
                                          Address Information
                                      &lt;/div&gt;
                                      &lt;form method=&quot;post&quot;&gt;
                                          &lt;div class=&quot;accordion__body&quot;&gt;
                                              &lt;div class=&quot;bilinfo&quot;&gt;
                                                  
                                                      &lt;div class=&quot;row&quot;&gt;
                                                          &lt;div class=&quot;col-md-12&quot;&gt;
                                                              &lt;div class=&quot;single-input&quot;&gt;
                                                                  &lt;input type=&quot;text&quot; name=&quot;address&quot; placeholder=&quot;Street Address&quot; required&gt;
                                                              &lt;/div&gt;
                                                          &lt;/div&gt;
                                                          &lt;div class=&quot;col-md-6&quot;&gt;
                                                              &lt;div class=&quot;single-input&quot;&gt;
                                                                  &lt;input type=&quot;text&quot; name=&quot;city&quot; placeholder=&quot;City/State&quot; required&gt;
                                                              &lt;/div&gt;
                                                          &lt;/div&gt;
                                                          &lt;div class=&quot;col-md-6&quot;&gt;
                                                              &lt;div class=&quot;single-input&quot;&gt;
                                                                  &lt;input type=&quot;text&quot; name=&quot;pincode&quot; placeholder=&quot;Post code/ zip&quot; required&gt;
                                                              &lt;/div&gt;
                                                          &lt;/div&gt;
                                                          
                                                      &lt;/div&gt;
                                                  
                                              &lt;/div&gt;
                                          &lt;/div&gt;
                                          &lt;div class=&quot;&lt;?php echo $accordion_class?&gt;&quot;&gt;
                                              payment information
                                          &lt;/div&gt;
                                          &lt;div class=&quot;accordion__body&quot;&gt;
                                              &lt;div class=&quot;paymentinfo&quot;&gt;
                                                  &lt;div class=&quot;single-method&quot;&gt;
                                                      COD &lt;input type=&quot;radio&quot; name=&quot;payment_type&quot; value=&quot;COD&quot; required/&gt;
                                                      &amp;nbsp;&amp;nbsp;PayPal &lt;input type=&quot;radio&quot; name=&quot;payment_type&quot; value=&quot;paypal&quot; onclick=&quot;showButton()&quot; required/&gt;
                                                  &lt;/div&gt;
                                                  
                                                 &lt;!-- Set up a container element for the button --&gt;
                                                  &lt;div id=&quot;paypal-button-container&quot;&gt;&lt;/div&gt;
  
                                                  &lt;!-- Include the PayPal JavaScript SDK --&gt;
                                                  &lt;script src=&quot;https://www.paypal.com/sdk/js?client-id=ASCkJmx1-2CeTlk1ABMrnC4xJbalWvyLbv-mIx76k9644aZb49wX82Qrgcn6W15Y3Yvm6GrrfpehN_ib&amp;currency=USD&quot;&gt;&lt;/script&gt;
                                                          &lt;script&gt;
                                                           document.getElementById(&quot;paypal-button-container&quot;).style.display = &quot;none&quot;;
                                                          function showButton() {
                                                            var x = document.getElementById(&quot;paypal-button-container&quot;);
                                                            if (x.style.display === &quot;none&quot;) {
                                                              x.style.display = &quot;block&quot;;
                                                            } else {
                                                              x.style.display = &quot;none&quot;;
                                                            }
                                                          }
                                                          &lt;/script&gt;
                                                  
                                                  &lt;script&gt;
                                                  
  var num = document.getElementById(&quot;totalprice&quot;).value;                                                    
                                                      // Render the PayPal button into #paypal-button-container
                                                      paypal.Buttons({
  
                                                          // Set up the transaction
                                                          createOrder: function(data, actions) {
                                                              return actions.order.create({
                                                                  purchase_units: [{
                                                                      amount: {
                                                                          value: num
                                                                      }
                                                                  }]
                                                              });
                                                          },
  
                                                          // Finalize the transaction
                                                          onApprove: function(data, actions) {
                                                              return actions.order.capture().then(function(details) {
                                                                  // Show a success message to the buyer
                                                                  alert('Transaction completed by ' + details.payer.name.given_name + '!');
                                                              });
                                                          }
  
  
                                                      }).render('#paypal-button-container');
                                                  &lt;/script&gt;
                                                  &lt;div class=&quot;single-method&quot;&gt;
                                                    
                                                  &lt;/div&gt;
                                              &lt;/div&gt;
                                          &lt;/div&gt;
                                           &lt;input type=&quot;submit&quot; name=&quot;submit&quot;/&gt;
                                      &lt;/form&gt;
                                  &lt;/div&gt;
                              &lt;/div&gt;
                          &lt;/div&gt;
                      &lt;/div&gt;
                      &lt;div class=&quot;col-md-4&quot;&gt;
                          &lt;div class=&quot;order-details&quot;&gt;
                              &lt;h5 class=&quot;order-details__title&quot;&gt;Your Order&lt;/h5&gt;
                              &lt;div class=&quot;order-details__item&quot;&gt;
                                  &lt;?php
                                  $cart_total=0;
                                  foreach($_SESSION['cart'] as $key=&gt;$val){
                                  $productArr=get_product($connection,'','',$key);
                                  $pname=$productArr[0]['name'];
                                  $mrp=$productArr[0]['mrp'];
                                  $price=$productArr[0]['price'];
                                  $image=$productArr[0]['image'];
                                  $quantity=$val['qty'];
                                  $cart_total=$cart_total+($price*$quantity);
                                  ?&gt;
                                  &lt;div class=&quot;single-item&quot;&gt;
                                      &lt;div class=&quot;single-item__thumb&quot;&gt;
                                          &lt;img src=&quot;media/product/&lt;?php echo $image?&gt;&quot;  /&gt;
                                      &lt;/div&gt;
                                      &lt;div class=&quot;single-item__content&quot;&gt;
                                          &lt;a href=&quot;#&quot;&gt;&lt;?php echo $pname?&gt;&lt;/a&gt;
                                          &lt;span class=&quot;price&quot;&gt;&lt;?php echo $price*$quantity?&gt;&lt;/span&gt;
                                      &lt;/div&gt;
                                      &lt;div class=&quot;single-item__remove&quot;&gt;
                                          &lt;a href=&quot;javascript:void(0)&quot; onclick=&quot;manage_cart('&lt;?php echo $key?&gt;','remove')&quot;&gt;&lt;i class=&quot;icon-trash icons&quot;&gt;&lt;/i&gt;&lt;/a&gt;
                                      &lt;/div&gt;
                                  &lt;/div&gt;
                                  &lt;?php } ?&gt;
                              &lt;/div&gt;
                              &lt;div class=&quot;ordre-details__total&quot;&gt;
                                  &lt;h5&gt;Order total&lt;/h5&gt;
                                  &lt;span class=&quot;price&quot;&gt;&lt;?php echo $cart_total?&gt;&lt;/span&gt;
                              &lt;/div&gt;
                          &lt;/div&gt;
                      &lt;/div&gt;
                  &lt;/div&gt;
              &lt;/div&gt;
          &lt;/div&gt;
                                  
  &lt;?php require('footer.php')?&gt;    
  </code></pre>