0

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>

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.

4
  • 1
    did you view the source code? looks fine to me.... although i'd generate the whole URL in PHP and put it in the href. Commented Jun 1, 2011 at 2:02
  • You would need to setup a demo page so we can see the generated code. But I agree with Mark, looks okay. Commented Jun 1, 2011 at 2:05
  • Check the generated code. What does $nid and $pnid contain? Commented Jun 1, 2011 at 2:05
  • are those variables actually set? View source! Commented Jun 1, 2011 at 2:17

3 Answers 3

2

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. 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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks that worked. I see now that when using onclick I need to use onclick=\"..whatever..\" for now on within echo. It causes problems when I don't.
2

You could always try:

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

3 Comments

This is certainly better, but should not make a difference in this case.
@Felix: since there is no perfectly robust way of doing things, and when something that should be true being false, we need to look at different ways and approaches to deal with it, even though they look silly. Note that this website is for providing all the possibilities to the problems. Have you ever experienced a silly problem? And the answer that sounds silly can fix it. Upvoted again (:
@Being: I totally agree with you. FWIW, I did not down vote. I just wanted to point out that there is no difference regarding the output between this code and the OP's code. So although being better, it does not solve the problem...
0

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

<script type="text/javascript">
function getnt(element) {
    var href = element.href;
    var nid = element.getAttribute("data-nid");
    var pnid = element.getAttribute("data-pnid");
    return true;
}
</script>
<p><a href="nt.php?nid=<?php echo $nid ?>&amp;pnid=<?php echo $pnid ?>"
      data-nid="<?php echo $nid ?>"
      data-pnid="<?php echo $pnid ?>"
      onclick="return getnt(this)">VIEW</a></p>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.