0

So I have this line of code in a php file:

echo "<a class='button' onclick=report('$row[id]')></a>";

I'd like to add an id to the line, so I tried this:

echo "<a class='button' id='"report".$row[id]' onclick=changeColor('$row[id]')></a>";

and in my javascript file

document.getElementById('report'+id).style.color('blue');

but I keep getting errors in javascript

cannot read property 'style' of null,

because I'm doing something wrong with the concatenation. I'm trying to get the id to be something like "report100" by concatenating it with $row[id] and then in the javascript file, change it's font color. I've tried other variations, but I just seem to get it right.

hakre
  • 193,403
  • 52
  • 435
  • 836
Jon
  • 319
  • 1
  • 4
  • 19
  • you getting id correctly in js ? – US-1234 Apr 05 '15 at 04:38
  • Yes, I'm getting the error cannot read property 'style' of null, which I think points to where I'm doing the concatenation – Jon Apr 05 '15 at 04:41
  • The PHP code already gives an error ***"PHP Parse error: syntax error, unexpected 'report' (T_STRING), expecting ',' or ';'"***. You have to enable error reporting and logging in PHP already and then track for such errors. PHP then tells you where you've made a little mistake: [How to get useful error messages in PHP?](http://stackoverflow.com/q/845021/367456). Working with strings in PHP is also well outlined in the PHP manual her: http://php.net/string - good luck and have fun. – hakre Apr 05 '15 at 07:03

2 Answers2

2

try like this.

echo "<a class='button' id='report".$row['id']."' onclick=changeColor(".$row['id'].")></a>";

UPDATE :

To concatenate a string and variable together, use the string concatenation operator . (a dot).

As well it is depend on how you start the string

Starting the string with '' (single quotes) will be

echo '<a class="button" id="report'.$row['id'].'">';

Starting the string with "" (double quotes) will be

echo "<a class='button' id='report'".$row['id'].">";
hakre
  • 193,403
  • 52
  • 435
  • 836
Raja
  • 600
  • 6
  • 15
0

The to prevent the " from terminating the string, you can use \".

echo "<a class=\"button\" id=\"report$row[id]\" onclick=\"changeColor('$row[id]')\"></a>";

Should result in

<a class="button" id="report100" onclick="changeColor('100')"></a>
Wio
  • 1,197
  • 10
  • 9