简体   繁体   中英

Passing Javascript variable to PHP link

I have a PHP link as follows, which appears inside a modal:

<?php echo $this->Html->link('Cart', ['controller' => 'payments', 'action' => 'cart', ]); ?>

I also have a script that brings up that modal:

$('.pay').click(function (ev) {
    ev.preventDefault();
    $('#payModal').modal('show');
    var bookingId = $(this).attr('data-id');
 });

I would like to try and get the JavaScript variable bookingId to be passed to the PHP link. I thought of doing it via a form, but I'm not submitting anything, so nothing would show up when doing a post/request.

I depends if you have other GET Parameters in your HTTP request. If there are none you can navigate to the current path and append your desired GET Parameter.

window.location = '?booking_id=' + bookingId; //navigate to the current page
window.location = './othersite.php?booking_id=' + bookingId; //navigate to another page

This will navigate to the current page with just the booking_id as GET parameter.

If you want to keep your other parameters you have to parse the current URL, append your paramteter, then serialize it back into an URL and change the location to it.

Just to clarify some things about relative links here:

//lets take the following URL as an example
'https://www.example.com/blog/pages/2984?id=3'

''       //current page --> 'https://www.example.com/blog/pages/2984'
'./'     //current 'directory' you are in --> 'https://www.example.com/blog/pages'
'../'    //parent 'directory' --> 'https://www.example.com/blog'
'../../' //2nd parent 'directory' --> 'https://www.example.com'
'/'      //root 'directory' --> 'https://www.example.com'

In the modal script, I added in the following:

$('.pay').click(function (ev) {
    ev.preventDefault();
    var bookingId = $(this).attr('data-id');
    $('#payModal').modal('show'); 
    $('#payModal').attr('bookingId',bookingId); //added in this line, setting the bookingId variable as an attribute of the modal.
});

I then changed my PHP link to a standard HTML button:

<button onclick="cartredirect()">Proceed to Cart</button>

This then triggers a second JavaScript:

<script>
     function cartredirect(){
          var bookingId = $("#payModal").attr('bookingId'); //returns the modal attribute from before.
          window.location = "<?= $host . $basepath ?>/payments/cart/" + bookingId; //now as a result, the page with the correctly appended variable can load.
     };
</script>

**

As You Already know PHP and Js Are Both Script languages and they bind to a variable as they execute , so with the information you gave for your js use

**

$('.pay').on ('click',function (ev) {
ev.preventDefault();
    $('#payModal').modal('show');
    var bookingId = $(this).attr('data-id');
 });

if this doesn't work generate you link in js with append to html

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