简体   繁体   English

将特定的Javascript变量传递给PHP

[英]Passing specific Javascript variable to PHP

I have a multi dimensional session array in PHP( $_SESSION['playlist']['ID1'] , $_SESSION['playlist']['ID2'] ETC) with IDs stored as values. 我在PHP中有一个多维会话数组( $_SESSION['playlist']['ID1']$_SESSION['playlist']['ID2'] ETC),其ID存储为值。 In javascript these can be deleted with the press of a button which will relay the value to a function in PHP to tell it to remove that value from the session array by matching the given ID and the one stored in the array. 在javascript中,可以通过按一下按钮将其删除,该按钮会将值中继给PHP中的一个函数,以告诉它通过匹配给定ID和存储在数组中的ID从会话数组中删除该值。 I can't get this to work. 我无法使它正常工作。 My JS is sending this $(wrapper).append("<?php delVid('"+vidID+"');?> "); 我的JS正在发送此$(wrapper).append("<?php delVid('"+vidID+"');?> "); but PHP acts like JS is sending a string, which I thought the quotes would make it do anyway but it was the only way I could get the variable to even properly transfer to PHP. 但是PHP的行为就像JS发送一个字符串一样,我认为引号会使它正常工作,但这是我获取变量甚至将其正确传输到PHP的唯一方法。 here's my PHP function 这是我的PHP函数

function delVid($id){
    foreach( $_SESSION['playlist'] as $key => $value ) {
            if($id == $value){
                unset( $_SESSION['playlist'][ $key ] );
            }
        sort($_SESSION['playlist'], SORT_NUMERIC);
    }
}

It doesn't work the way you suppose. 它不像您想象的那样工作。 Appending HTML with JQuery doesn't run your server-side PHP script. 用JQuery附加HTML不会运行您的服务器端PHP脚本。 So it is just a string appended to the wrapper which has no sense from code execution's point of view. 因此,这只是附加到wrapper的字符串,从代码执行的角度来看,这没有任何意义。

To make what you want to do, you need to use Ajax technology which consists in passing data to server without page reload. 为了完成您想做的事情,您需要使用Ajax技术,该技术包括在不重新加载页面的情况下将数据传递到服务器。 So you need to create a server side page which will accept your $id parameter and invoke your delVid function. 因此,您需要创建一个服务器端页面,该页面将接受$id参数并调用delVid函数。

// ajaxScript.php (pseudo code)
// I don't know what framework and routing scheme are you using,
// so just a raw code to show an idea

<?php       

function delVid($id) {
    foreach($_SESSION['playlist'] as $key => $value) {
        if($id == $value) {
            unset($_SESSION['playlist'][$key]);
        }
        sort($_SESSION['playlist'], SORT_NUMERIC);
    }
}

delVid($_POST['id']);

Then you need to write JS code which will be perform Ajax request to that page on some event. 然后,您需要编写JS代码,以便在某些事件下对该页面执行Ajax请求。 JQuery has such useful methods as $.ajax , $.get and $.post to implement it. JQuery具有$.ajax$.get$.post这样有用的方法来实现它。

jQuery(document).ready(function($){ 
  $(document).on("click",".remove", function(){ 
    var vidID = $(this).parent('div').parent('div').attr('id');             
    $(this).parent('div').parent('div').fadeOut();
    $.post('/ajaxScript.php', {id:vidId});
  }); 
});

You are trying to append to HTML a PHP function, which shows that you havem't understood how the client-server model works. 您试图将HTML函数附加到HTML,这表明您不了解客户端服务器模型的工作方式。 PHP is a server side script, while JS and HTML are client side: so what you have to do is to use AJAX to send a request to your server with the proper parameters. PHP是服务器端脚本,而JS和HTML是客户端端:因此,您要做的就是使用AJAX通过适当的参数向服务器发送请求。 But first of all you have to understand how PHP and JS work. 但是首先,您必须了解PHP和JS的工作方式。

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

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