-2

I have a php file and in that file it consist of a code like below

<?php if ($totalBookings !== 1) { ?>
  <div class="modal-body mundana-login-wrapper">
     <label>Are you sure you want to remove this booking?</label>
  </div>
  <div class="modal-footer">
     <button type="button" onclick="removeBooking()" id="remove_booking" class="mundana-button primary ghost" data-dismiss="modal">Yes</button>
     <button type="button" onclick="cancelRemoveBooking();" class="mundana-button ghost" data-dismiss="modal">Cancel</button>
  </div>
  <?php } else { ?>
  <div class="modal-body mundana-login-wrapper">
     <label>You are about to remove the only selected room. Do you want to remove?</label>
  </div>
  <div class="modal-footer">
     <button type="button" onclick="removeBooking();" id="remove_booking" class="mundana-button primary ghost" data-dismiss="modal">Yes</button>
     <button type="button" onclick="cancelRemoveBooking();" class="mundana-button ghost" data-dismiss="modal">Cancel</button>
  </div>
<?php } ?>

In the same file i have a code inside script tags as follows

<script>

   function getCookie(name) {
       const value = `; ${document.cookie}`;
       const parts = value.split(`; ${name}=`);
       if (parts.length === 2) return parts.pop().split(';').shift();
   }

   function updateCookie(){
       $totalBookings = getCookie('total_bookings');
   }
</script>

I want to access the $totalBookings which is declared inside the script tags, to the if condition i have used in the php file. <?php if ($totalBookings !== 1) { ?>

I cannot access that variable like that and i'm new to php. How can i access the $totalBooking variable which is inside the script tags to my php code.?

CraZyDroiD
  • 6,622
  • 30
  • 95
  • 182
  • 1
    you know that php is server side and js is client side so you cannot mix them in any way? by the way $totalBookings shoud be totalBookings (it's php where variables start with $). For what you want to do either access the cookie via php and assign the variable or use ajax – Lelio Faieta May 12 '20 at 14:50
  • Does this answer your question? [How do I pass variables and data from PHP to JavaScript?](https://stackoverflow.com/questions/23740548/how-do-i-pass-variables-and-data-from-php-to-javascript) – Abdulaziz Yesuf May 12 '20 at 14:52
  • the issue is the cookie value changes everytime and when accessing the cookie value via php, i always get the initial cookie value and not the updated value – CraZyDroiD May 12 '20 at 14:52
  • You need to read about Front-End and Back-End, JavaScript is executed in browser it can't access php code and php code can't access JavaScript they are executed in completely different times, php on the server and JS in the browser after php was already generated. If you want to access JS variable inside php you need to use AJAX. – jcubic May 12 '20 at 14:52
  • So if you want to modify the cookie you need to send request to the server that will change the cookie value `$totalBookings = getCookie('total_bookings');` this line is executed in browser not on the server. – jcubic May 12 '20 at 14:53
  • @CraZyDroiD I was going to add the "cookies" tag. Do you think it's relevant to the question? – Funk Forty Niner May 12 '20 at 14:54

2 Answers2

-1

You can communicate easily between PHP and JS using cookies. In JavaScript, define a cookie (document.cookie="foo=bar") then retrieve it in php (echo $_COOKIE["foo"]).

fig
  • 364
  • 2
  • 11
-3

Here is an example First you should make a js var which is total

var total = 1;

then assign it to php var as shown

$total = <script>document.writeln(total)<script>;

Now you can do whatever you want with this var