Javascript函数接受PHP变量 [英] Javascript Function Accepting PHP Variables

查看:68
本文介绍了Javascript函数接受PHP变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在弄清楚为什么这不起作用。我可以通过一个变量通过,但不是两个。当我使用像 getnt(1,2)这样的实际数字时,它可以工作。它只是不使用两个PHP变量。

I'm drawing a complete blank why this isn't working. I can do it with one variable passing through, but not two. When I use actually numbers like getnt(1,2) it works. It's just not working with two PHP variables.

  <script type="text/javascript">
  function getnt(nid,pnid) {
      window.location = "nt.php?nid=" + nid + "&pnid=" + pnid;
  }
  </script>
  <body>  
  <?php
    echo "<a href='#' onclick='getnt($nid,$pnid)'>VIEW</a>";
  ?>
  </body>

我可以使用 echo< a href =' nt.php?nid = $ nid& pnid = $ pnid'> VIEW< / a>; ,但如果我想添加警报和javascript命令,那就不好了。

I can make the code work with echo "<a href='nt.php?nid=$nid&pnid=$pnid'>VIEW</a>";, but that's no good if I want to add in alerts and javascript commands.

推荐答案

如果ID和pnID是字符串,请用这样的括号括起来。

If the ID and pnID are strings, enclose them with brackets like this.

<body>  
  <?php
    echo "<a href='#' onclick=\"getnt('$nid','$pnid')\">VIEW</a>";
  ?>
  </body>

如果仍然无法正常工作,您可以调试代码

If still not working, You can debug your code


  1. 在浏览器中查看源代码,
    确保它生成正确。

  2. 在$ b中添加一些警报消息$ b javascript函数。如果你有Firefox,请安装Firebug
    或者看看

  1. View the source code in browser, make sure it generates correctly.
  2. Put some alert messages in the javascript function. Install Firebug if you have Firefox or see

如果你有任何javascript错误,请使用Javaascript控制台。

Javaascript console if you get any javascript errors.

这篇关于Javascript函数接受PHP变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆