0

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

3 Answers 3

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

echo "<a href=\"javascript:displayBigImage('".$row['IMG_ID']."')\">Press</a>";
Sign up to request clarification or add additional context in comments.

Comments

1

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>";

Comments

1

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>";

Comments

Your Answer

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