Skip to content Skip to sidebar Skip to footer

Javascript Function Accepting Php Variables

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

Solution 1:

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

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

If still not working, You can debug your code

  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

  3. Javaascript console if you get any javascript errors.

Solution 2:

You could always try:

<scripttype="text/javascript">functiongetnt(nid,pnid) {
        window.location = "nt.php?nid=" + nid + "&pnid=" + pnid;
    }
</script><body><ahref="#"onclick="getnt(<?phpecho$nid; ?>,<?phpecho$pnid; ?>)">VIEW</a></body>

Solution 3:

Your question is probably best answered by looking at the rendered HTML source.

In any case, here's how I'd do it using graceful degradation

<scripttype="text/javascript">functiongetnt(element) {
    var href = element.href;
    var nid = element.getAttribute("data-nid");
    var pnid = element.getAttribute("data-pnid");
    returntrue;
}
</script><p><ahref="nt.php?nid=<?phpecho$nid?>&amp;pnid=<?phpecho$pnid?>"data-nid="<?phpecho$nid?>"data-pnid="<?phpecho$pnid?>"onclick="return getnt(this)">VIEW</a></p>

Post a Comment for "Javascript Function Accepting Php Variables"