I'm trying to echo a dynamic a tag which calls a javascript function, but the parameters are not being echoed correctly. They should retain their capitalization and not add spacing. Why is it doing this?
I've tried removing variables and just echoing a straight string with what I want, but it still displays incorrectly.
What I need:
echo '<a href="'.$info[0].'" onClick="redirTrackCalcBtn("'.$bname.'", "'.$info[0].'")"><img src="'.$info[1].'"/></a>'
Pure String Version:
echo '<a href="/calc" onclick="redirTrackCalcBtn("Test_Button_1", "/calc")"><img src="/images/calc-eng-desktop.png"></a>'
Outputs:
<a href="/calc" onclick="redirTrackCalcBtn(" test_button_1",="" "="" calc")"="">
<img src="/images/calc-eng-desktop.png">
</a>
Should Output:
<a href="/calc" onclick="redirTrackCalcBtn("Test_Button_1", "/calc")">
<img src="/images/calc-eng-desktop.png">
</a>
I also tried:
echo "<a href=\"".$info[0]."\" onClick=\"redirTrackCalcBtn(\"".$bname."\", \"".$info[0]."\")\"><img src=\"".$info[1]."\"/></a>";
But that still outputs:
<a href="/calc" onclick="redirTrackCalcBtn(" test_banner_1",="" "="" calc")"=""><img src="/images/calc.png"></a>
as per Dharman's response I also Tried:
echo '<a href="'.$info[0].'"
onClick=\"redirTrackCalcBtn("'.$bname.'", "'.$info[0].'")\"
><img src="'.$info[1].'"/></a>'
This outputs:
<a href="/calc" onclick="\"redirTrackCalcBtn("Test_Banner_1"," "="" calc")\"="">
<img src="/images/preguntanos-h-es.png">
</a>
Edit for context: It's for a dynamic banner within the content of a blog powered by WordPress.
You can simplify your expressions using the following technique ...
So based on this, the answer would be:
<?php
echo "<a href='{$info[0]}' onClick='redirTrackCalcBtn(\"{$bname}\", \"{$info[0]}\")'><img src='{$info[1]}'/></a>";
This will give the following result ...
<a href='/calc' onClick='redirTrackCalcBtn("test_button_1", "/calc")'><img src='/images/calc-eng-desktop.png'/></a>
In your question, you have shown an Pure String Version and what you thought was a normal output. Both of those outputs are wrong. You cannot use something like onclick="redirTrackCalcBtn("Test_Button_1", "/calc")"
because the double quote right after the opening parenthesis finishes the onclick attribute which become onclick="redirTrackCalcBtn("
. After that, the browser will try its best to find the following attributes and their values. So the spaces that you are seeing are just the natural space between attributes.
In conclusion, there is nothing wrong with echo
.