简体   繁体   English

将Javascript变量传递给PHP链接

[英]Passing Javascript variable to PHP link

I have a PHP link as follows, which appears inside a modal: 我有一个如下的PHP链接,它出现在一个模态中:

<?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. 我想尝试将JavaScript变量bookingId传递给PHP链接。 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. 我依赖你的HTTP请求中是否有其他GET参数。 If there are none you can navigate to the current path and append your desired GET Parameter. 如果没有,您可以导航到当前路径并附加所需的GET参数。

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. 这将导航到当前页面,只有booking_id作为GET参数。

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. 如果要保留其他参数,则必须解析当前URL,附加参数,然后将其序列化为URL并将位置更改为该参数。

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: 然后我将PHP链接更改为标准HTML按钮:

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

This then triggers a second JavaScript: 然后,这会触发第二个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 因为您已经知道PHP和Js都是脚本语言,并且它们在执行时绑定到变量,所以使用您为js使用的信息

** **

$('.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 如果这不起作用生成你链接在js与追加到HTML

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM