简体   繁体   English

如何将 JavaScript 值传递给 Ajax 内的 PHP

[英]How to pass JavaScript values to PHP within the Ajax

Here is the situation.这是情况。

I'm trying to pass from some Javascript values to various PHP functions within my ajax so it can properly be displayed on the page.我正在尝试将一些 Javascript 值传递给 ajax 中的各种 PHP 函数,以便它可以正确显示在页面上。

Here is my code:这是我的代码:

       $("[data-department-id]").click(function() {                   
                id = $(this).attr('data-department-id');     
                document.getElementById('department'+id).innerHTML = '';                  
                $.ajax({
                    url:"/desk/template/fetchtickets.php",
                    type: 'POST', 
                    dataType: 'json',                      
                    data : {
                      'id' : id  
                    },
                    success: function (res) {                            
                        $('#department'+id).append('<ul>');
                        for (var key in res) { 
                            
                            var ticketId = res[key].id;
                            var clientId = res[key].clientid;
                            var clientName = res[key].name;
                            var clientColor = res[key].hexColor;    
                            
                           <?php  $ticketId = '"+ticketId+"';   ?>   
                           <?php  $clientId = '"+clientId+"';   ?>   
                           <?php  $clientName = '"+clientName+"';   ?>   
                           <?php  $clientColor = 'clientColor';   ?> 
                           
                           $('#department'+id).append('<li class="list-group-item list-group-item-light" data-date-2="<?php echo Ticket::lastReplyStamp($ticketId); ?>"> <span class="text-width"><a href="?route=tickets/manage&id='+res[key].id+'">'+res[key].ticket+'  '+res[key].subject+'</a></span><div class="tools" ><span class="client-listing"> <?php clientBadge($clientId,$clientName,$clientColor); ?>    <?php // echo "<scipt>document.writeln(test)</script>";    ?> '+test+'      </div></div></li>');
                        }                     
                        $('#department'+id).append('</ul>');  
                    }
                });                                       
            })

When I do a console.log();当我做一个 console.log();

It shows the proper value, however, when I do an echo $ticketId, it shows +ticketId+.它显示了正确的值,但是,当我执行 echo $ticketId 时,它显示 +ticketId+。

Now I did do a document.write and document.writeln and it still doesn't work.现在我确实做了一个 document.write 和 document.writeln ,但它仍然不起作用。

Is there a proper solution to resolve my problem?有没有合适的解决方案来解决我的问题?

You can not add php code here.You can use JQuery to change value in the Dom or set some hidden inputs value, but you can not set php variable in JS.您不能在此处添加 php 代码。您可以使用 JQuery 更改 Dom 中的值或设置一些隐藏的输入值,但您不能在 JS 中设置 php 变量。 PHP runs on the server side. PHP 在服务器端运行。 When the php code is running, your js code is waiting to be run on the client's computer.当 php 代码运行时,您的 js 代码正在等待在客户端计算机上运行。

These line are always going to be interpreted as string assign from server side.这些行总是将被解释为从服务器端分配的字符串。

<?php  $ticketId = '"+ticketId+"';   ?>   
<?php  $clientId = '"+clientId+"';   ?>   
<?php  $clientName = '"+clientName+"';   ?>   
<?php  $clientColor = 'clientColor';   ?> 

The order of your code processing is something like this:您的代码处理顺序是这样的:

  1. PHP code get processed PHP 代码得到处理
  2. PHP returns HTML PHP 返回 HTML
  3. User clicks an element (in your case data-department-id )用户单击一个元素(在您的情况下data-department-id
  4. JQuery sends ajax request JQuery 发送 ajax 请求
  5. Server processes the request and sends response back to the frontend服务器处理请求并将响应发送回前端
  6. Ajax success function receives the response and now has the response in javascript variable res which is passed as argument of success(res) function Ajax 成功 function 收到响应,现在在 javascript 变量res中有响应,作为success(res) ZC1C42507C17838
  7. All the code inside success is executed执行成功里面的所有代码

Alternatively if the server throws an error response, no 6. and 7. would trigger the error callback或者,如果服务器抛出错误响应,则 6. 和 7. 不会触发错误回调

But the main part it, the PHP code in your success() function will NOT run as you are thinking, which is after the Ajax receives response.但它的主要部分,你的成功()中的 PHP 代码 function 不会像你想的那样运行,这是在 Ajax 收到响应之后。 At this point all you can do is use javascript to manipulate the variables.此时您所能做的就是使用 javascript 来操作变量。

So in short, change below part and use javascript or jQuery if you have it to change DOM elements based on response.因此,简而言之,更改下面的部分并使用 javascript 或 jQuery 如果您有根据响应更改 DOM 元素。

<?php  $ticketId = '"+ticketId+"';   ?>   
...
...

For example, let's say you have an H1 tag which shows ticket id.例如,假设您有一个显示票证 ID 的 H1 标签。 You would do你会做

// jQuery
$('#ticketIdElement').text(res.id);

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

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