如何从 PHP 函数进行简单的 JavaScript 调用?

发布于 2024-11-01 14:50:47 字数 505 浏览 0 评论 0原文

我有一个简单的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

可爱暴击 2024-11-08 14:50:48

您知道 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>".

是伱的 2024-11-08 14:50:48

让您的函数返回输出并将输出打印在 GOOGLE 代码

你不能只输出 JS 并期望它在某个地方自行触发......或者让 PHP 调用 Google。

  • PHP 是服务器端
  • JS 是客户端(来自 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.

  • PHP is SERVER SIDE
  • JS is CLIENT SIDE (clients browser requests from google, not your server)

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.

奢华的一滴泪 2024-11-08 14:50:48

您只需从表单的 onSubmit 参数调用 javascript 代码即可。

<form name="..." action="..." onSubmit="_gaq.push(['_trackPageview', 'virtual/formSubmit']);">

You just have to call your javascript code from the onSubmit argument of the form.

<form name="..." action="..." onSubmit="_gaq.push(['_trackPageview', 'virtual/formSubmit']);">
苍风燃霜 2024-11-08 14:50:48
echo " <script type='text/javascript'>_gaq.push(['_trackPageview', 'virtual/formSubmit']); </script> ";
echo " <script type='text/javascript'>_gaq.push(['_trackPageview', 'virtual/formSubmit']); </script> ";
不…忘初心 2024-11-08 14:50:48

你不能。 JavaScript 是客户端,PHP 是服务器端。

您可以让浏览器调用 JavaScript 函数:

<?='<script type="text/javascript">jsfunc();<;/script>';?>;

You can't. JavaScript is client side, and PHP is server side.

You can make the browser call a JavaScript function:

<?='<script type="text/javascript">jsfunc();<;/script>';?>;
枯叶蝶 2024-11-08 14:50:48

简短而简单的答案是您无法在服务器端执行 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.).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文