admin管理员组

文章数量:1336660

I am running the < a > tag in php. Whenever I pass an argument in js function it does not get called but if i pass empty arguments, the function gets called.

js:

function displayBigImage(img){
  alert("inside func");
}

php:

//NOT WORKING:
echo "<a href='javascript:displayBigImage('".$row['IMG_ID']."')'>Press</a>";

//WORKING:
echo "<a href='javascript:displayBigImage()'>Press</a>";

I also tried with harcode argument values like,

echo "<a href='javascript:displayBigImage('sample.jpg')'>Press</a>";

or

echo "<a href='javascript:displayBigImage(sample.jpg)'>Press</a>";

I don't understand whats wrong?!?!?!?!

PLease reply asap.

Thanks in advance

I am running the < a > tag in php. Whenever I pass an argument in js function it does not get called but if i pass empty arguments, the function gets called.

js:

function displayBigImage(img){
  alert("inside func");
}

php:

//NOT WORKING:
echo "<a href='javascript:displayBigImage('".$row['IMG_ID']."')'>Press</a>";

//WORKING:
echo "<a href='javascript:displayBigImage()'>Press</a>";

I also tried with harcode argument values like,

echo "<a href='javascript:displayBigImage('sample.jpg')'>Press</a>";

or

echo "<a href='javascript:displayBigImage(sample.jpg)'>Press</a>";

I don't understand whats wrong?!?!?!?!

PLease reply asap.

Thanks in advance

Share Improve this question edited Aug 22, 2009 at 21:43 Gumbo 656k112 gold badges791 silver badges851 bronze badges asked Aug 22, 2009 at 21:39 sbsb
Add a ment  | 

3 Answers 3

Reset to default 8

You have problems with your quoting:

<a href='javascript:displayBigImage('sample.jpg')'>

You can't use single quotes both around the HTML attribute and within it. You need to use different quotes in the two places, for instance:

<a href="javascript:displayBigImage('sample.jpg')">

So in your PHP, that bees:

echo "<a href=\"javascript:displayBigImage('".$row['IMG_ID']."')\">Press</a>";

You have some mismatched quote marks. Where you have this:

echo " < a href='javascript:displayBigImage('".$row['IMG_ID']."')'>Press< / a >";

You should have this:

echo " <a href=\"javascript:displayBigImage('" . $row['IMG_ID'] . "')\">Press</a>";

If you’re using single quotes for the HTML attribute value declaration, you can’t use the same quotes inside the attribute values without describing them by character references.

So you either use the double quotes inside your href attribute value:

echo "<a href='javascript:displayBigImage(\"".$row['IMG_ID']."\")'>Press</a>";

Or you use proper character references:

echo "<a href='javascript:displayBigImage(&#27;".$row['IMG_ID']."&#27;)'>Press</a>";

Or you use the double quotes for the href attribute value declaration:

echo "<a href=\"javascript:displayBigImage('".$row['IMG_ID']."')\">Press</a>";

本文标签: javascript function not get called when arguments from phpStack Overflow