views:

419

answers:

3

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

+7  A: 

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 becomes:

echo "<a href=\"javascript:displayBigImage('".$row['IMG_ID']."')\">Press</a>";
RichieHindle
+1  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>";
VoteyDisciple
+1  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>";
Gumbo
thanks...u are right :D