如何从 PHP 函数进行简单的 JavaScript 调用?
我有一个简单的 PHP 函数,在提交表单时被调用:
function uc_paypal_ec_submit_form($form, &$form_state) {
$form['actions'] = array('#type' => 'actions');
$form['actions']['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit order'),
);
return $form;
}
我需要的是能够执行一个简单的 Google Analytics 调用,如下所示:
_gaq.push(['_trackPageview', 'virtual/formSubmit']);
我尝试了一些选项,但没有任何效果。我没有看到对 Google Analytics(分析)的调用...
我该如何解决此问题?
I have a simple PHP function that is being called when a form is submitted:
function uc_paypal_ec_submit_form($form, &$form_state) {
$form['actions'] = array('#type' => 'actions');
$form['actions']['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit order'),
);
return $form;
}
What I need is to be able to do a simple Google Analytics call like this:
_gaq.push(['_trackPageview', 'virtual/formSubmit']);
I've tried a few options, but nothing works. I don't see the call being made to Google Analytics...
How can I fix this problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您知道 JavaScript 运行在客户端浏览器中,而 PHP 运行在服务器上吗?
无论如何,现在你做到了。所以这应该回答你的问题。
只需回显
"
。Do you know that JavaScript is running in the client's browser while PHP is running on the server?
Anyway, now you do. So that should answer your question.
Simply echo
<script type="text/javascript">yourJsCodeHere</script>"
.让您的函数返回输出并将输出打印在 GOOGLE 代码
元素中,将使
_gaq.push...
触发。你不能只输出 JS 并期望它在某个地方自行触发......或者让 PHP 调用 Google。
所以就像我说的,将 JS 输出到触发 onload 的函数,然后就全部准备好了。
保存输出:
$output = "_gaq.push(['_trackPageview', 'virtual/formSubmit']);";
然后在某个地方回显它以触发它。
Make your function return the output and have the output print in the GOOGLE code
<script>
element, will make the_gaq.push...
fire off.You can't just output that JS and expect it to fire by itself somewhere... or for PHP to make a call to Google.
So like I said, make the JS output to a function that fires onload and you are all set.
save the output:
$output = "_gaq.push(['_trackPageview', 'virtual/formSubmit']);";
and then echo it somewhere to fire it.
您只需从表单的 onSubmit 参数调用 javascript 代码即可。
You just have to call your javascript code from the onSubmit argument of the form.
你不能。 JavaScript 是客户端,PHP 是服务器端。
您可以让浏览器调用 JavaScript 函数:
You can't. JavaScript is client side, and PHP is server side.
You can make the browser call a JavaScript function:
简短而简单的答案是您无法在服务器端执行 JavaScript 代码。漫长而艰难的答案是你可以,但是它又长又困难,并且涉及一些服务器端脚本/库,并且充其量是有点混乱。
基本上,您想要做的就是在用户登陆后的确认/感谢页面上输出该代码(最佳),或者使用 cURL 来进行点击(这有点棘手......基本上你必须模拟
img
请求,附加相关信息,包括 cookie 信息......注意,这不是上面提到的“又长又难”的方法。)。The short and easy answer is you can't execute JavaScript code server-side. The long and hard answer is you can, but it's long and hard and involves some server-side scripts/libraries and is kind of messy at best.
Basically, what you want to do is either output that code on the confirmation/thank you page the user lands on after (optimal) or else use cURL to make a hit (it is a little trickier... Basically you have to simulate the
img
request, appending the relevant information, including cookie information... Note, this is not the "long and hard" method mentioned above.).