-1

I have a JavaScript function, pop_item. I have to call this from PHP, so my PHP code is the following:

echo '<a href="javascript:pop_item('.$_code.',1)">Link </a>';

It provides no error, but pop_item is not functioning,

The HTML output for the above is:

<a href="javascript:pop_item('ABC',1)">Link </a>
4
  • What do you mean by "call from PHP"? Your PHP code will run on the server, and the Javascript code runs in the user's browser. Commented Dec 24, 2008 at 11:48
  • When you say not functioning...what exactly do you mean? Is the function running at all? Did you include the function on the page? Commented Dec 24, 2008 at 11:49
  • You should post the pop_item function, I think the problem is in there. Commented Dec 24, 2008 at 11:56
  • it doesn't return that value, you miss some quotes. Commented Dec 24, 2008 at 12:03

4 Answers 4

5

I think the problem is in the pop_item function since the call seems to be correct. Try this:

echo " <a href='#' onclick=\"pop_item(".$_code."', 1)\">link</a>";

Or

echo '<a href="javascript:alert('.$_code.')">Link</a>';

See if that works.

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

2 Comments

Judging from the HTML output, $_code already has quotes in it (assuming that the HTML output is accurate!).
Ah ok, that's possible, didn't think of that.
2

If your pop_item function accepts a string as its first parameter, this could happen because of missing some quote characters there. Use PHP's interpolation feature, so you could be sure which quote is which. Something like this line:

 echo "<a href=\"javascript: pop_item('$_code',1);\">Link</a>";

If the pop_item accepts some other data type, then the single quotes are needless. I also recommend to use you browser's JavaScript error console to see what the details of the problem are.

Comments

0

Your function is probably not defined... Make sure you included it somewhere...

1 Comment

on Firefox's Error Console, that would pop up an error of "pop_item() is not defined"
0

If the output looks correct, then PHP has done its job correctly and the problem is in your JavaScript code. Try running your page with Firebug, or some other JavaScript debugger to find the problem.

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.