1

HI i tring to call a js function in PHP file using echo ""; and it's not working, if I call it without the echo it's work, what am I missing here ?

this way it's is work:

<a href='#' onClick='delfrmvbar("dlsyg","<?php echo $sgId;?>","<?php echo $sgId;?>")'>X</a>&nbsp;</span> 

and this way is not (I tried without the - ' sign)

echo "<a href='#' onClick='delfrmvbar('dlsyg','$sgId','$sgId')'>X</a>&nbsp;</span>";
2
  • Why do you pass the same argument twice to your delfrmvbar function? Commented Jun 27, 2014 at 9:49
  • I am not, it's just for example, I just forgot to change the name of the third variable. Commented Jun 27, 2014 at 10:10

1 Answer 1

1

Your single quotes are the issue because you use single quotes to wrap the onclick attribute, but then use them again around the arguments.

Example solution: use double quotes around the attribute and escape.

echo "<a href='#' onClick=\"delfrmvbar('dlsyg','$sgId','$sgId')\">X</a>&nbsp;</span>";

This will render the HTML like below, which is syntactically correct:

<a href='#' onClick="delfrmvbar('dlsyg','xx','xx')">X</a>&nbsp;</span>
Sign up to request clarification or add additional context in comments.

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.