[英]How to pass javascript variables to php variables with yii
First: I KNOW this is a duplicate. 第一:我知道这是重复的。 I am creating this because none of the answers to all the other questions satisfy me.
我之所以这样做,是因为所有其他问题的答案都没有一个令我满意。 I have a very particular situation where I'm using yii and tryi I can't send it through ajax because the php and javascript are on the same page on the same page.
我有一个非常特殊的情况,我正在使用yii和tryi我无法通过ajax发送它,因为php和javascript位于同一页面的同一页面上。
Basically what I'm asking is if you have a page that uses yii and chart js, and you need to render a page that requires two arguments from the clicked bar, which is represented by activeBars[0]
: 基本上,我要问的是您是否有一个使用yii和chart js的页面,并且需要渲染一个页面,该页面需要单击栏中的两个自变量,由
activeBars[0]
:
<script>
canvas.onclick = function(event) {
var activeBars = getBarsAtEvent(event);
<?php $controller->functionThatRendersView($arg1 /**(activeBars[0].value*/,$arg2 /**(activeBars[0].label*/); ?>
}
I don't care if it will render automatically, that is another problem. 我不在乎它是否会自动渲染,这是另一个问题。 I just need to get those arguments to the php.
我只需要将这些参数传递给php。
Thanks. 谢谢。
Also, if it helps, I am passing those two values to javascript through php for loops: 另外,如果有帮助,我会将这两个值通过php for循环传递给javascript:
labels: [<?php for ($i=1;$i<=$numberIssues;$i++) {
echo $i . ",";
}?>],
The problem with grabbing $i
and putting it into the label argument is that I don't know which bar label is the clicked one, I need javascript to pass the clicked bar values back to php. 抓住
$i
并将其放入label参数的问题在于,我不知道哪个条形标签是被单击的条形标签,我需要Javascript将被单击的条形值传递回php。
Explain to us again why you can't use ajax. 再次向我们解释为什么您不能使用Ajax。 You say "because the php and javascript are on the same page".
您说“因为php和javascript位于同一页面上”。 That's not what ajax is - you need a different URL for the ajax request, and a separate PHP file or something to handle it.
那不是ajax是什么-您需要一个不同的URL来处理ajax请求,并需要一个单独的PHP文件或用于处理它的内容。
Without ajax it's impossible for javascript to send information to PHP, because the PHP runs on the server before the javascript runs on the client. 没有ajax,javascript就无法将信息发送到PHP,因为PHP在javascript在客户端上运行之前先在服务器上运行。 Unless of course you want to do a complete page refresh, which is slower and generally worse from the user perspective.
除非您当然要进行完整的页面刷新,否则从用户的角度来看,这比较慢,而且通常更糟。
I found an answer to my question! 我找到了问题的答案! I'm just doing this for anyone else who is stumbling:
我只是对其他绊脚的人这样做:
To pass javasctipt variable var jsInteger = 5;
传递javasctipt变量
var jsInteger = 5;
to php you type (in javascript): 到PHP您键入(在JavaScript中):
window.location.href = "yourPhpFile.php?phpInteger="+jsInteger;
You access the variable in php like so: 您可以像这样访问php中的变量:
$phpInteger = $_GET['phpInteger'];
This will require a page refresh, and will redirect you to the php file. 这将需要刷新页面,并将您重定向到php文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.