简体   繁体   English

PHP和JQuery传递变量

[英]PHP & JQuery passing variables

I am creating a plugin for WordPress and I have been stuck for a two days trying to figure a way of passing variables and it's values from jQuery to PHP, at the moment I have resorted to setting the boolean output of a conditional inside a cookie and then reading it with PHP in order to perform and update. 我正在为WordPress创建一个插件,我已经被困了两天试图找到一种方法来传递变量和从jQuery到PHP的值,目前我已经使用了在cookie中设置条件的布尔输出然后用PHP读取它以执行和更新。

jQuery I am using activeStatus to perform some conditionals in jQuery and then i am setting a cookie in order to continue performing other set of conditionals in PHP jQuery我使用activeStatus在jQuery中执行一些条件,然后我设置一个cookie,以便继续在PHP中执行其他条件集

var activeStatus = true;
jQuery.cookie('activeStatus', true, {expires: 1, path: '/'});

PHP now we read the cookie set by jQuery and perform update PHP现在我们读取jQuery设置的cookie并执行更新

if (!isset($_COOKIE["uniqueUser"])) { //unique visitor
            if ($_COOKIE["activeStatus"] == 'true') { //if cookie found and true
                $uniqueVisitor = get_option('stats');
                $uniqueVisitor['uniqueVisits']+=1;
                update_option('stats', $uniqueVisitor);
             }

The problem now is that the second if conditional will never be performed since cookies can only be read on the second refresh, hence, the update will never take place because the cookie would have been set. 现在的问题是第二个if条件永远不会被执行,因为cookie只能在第二次刷新时读取,因此,更新将永远不会发生,因为cookie已被设置。 for this reason I wish to pass the variable in real time. 因此我希望实时传递变量。

One more thing I would like to mention is that I am using PHP to print the whole javascript ie 我还想提一件事是我使用PHP来打印整个javascript即

<?php

add_action('exec_script', 'script');
function script() {
?>

<script type="text/javascript">
//code
</script>

<?php
}

To fit my purposes I need to print the js code in the page and cannot save it in a js file. 为了符合我的目的,我需要在页面中打印js代码,而不能将其保存在js文件中。

Anyone has any ideas? 有人有什么想法吗?

Just output your variables on the page inside a script tag like so: 只需在脚本标记内的页面上输出变量,如下所示:

<script>
  window.myvariable = "<?php $my_string ?>"
</script>

To do it the other way around I would suggest putting your variables in a post request. 反过来说,我建议将变量放在一个post请求中。 So if its a button the user is clicking do this: 因此,如果用户点击它的按钮,请执行以下操作:

<form action="/action.php">
  <input id="hidden_input" name="the_value" type="hidden" value="">
  <input type="submit" value="Click here to send variable">
</form>

<script>
  $("#hidden_input").value(javascript_variable);
</script>

You should set the cookie with a PHP script called with ajax. 您应该使用ajax调用的PHP脚本设置co​​okie。 Then when you load the next page, it will already be set for the PHP script to find it. 然后当你加载下一页时,它已经被设置为PHP脚本来找到它。 I admit I don't know the ins and outs of jquery.cookie so this may not be the answer. 我承认我不知道jquery.cookie的来龙去脉,所以这可能不是答案。 But this is what I'd start with. 但这就是我的开始。

I think you are counting the unique visitors so use 我想你是在计算独特的访客所以使用

if (!isset($_COOKIE["activeStatus"]) && !isset($_COOKIE['uniqueVisitor']))) { } 

It will increase the stats for first time and not after that 它将首次增加统计数据而不是之后

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

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