Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

how do i echo this php variable inside innerhtml of javascript. I tried this but it isn't working. the function isn't called because if the php code

<a href="#"><img src="images/group3.png" alt="groups" border="0" title="Groups" onclick="return false" onmousedown="showGroups()"></a>
<script type="text/javascript">
function showGroups() {
        _('groupModule').innerHTML = '<div id="groupWrapper"><div id="groupList"><h2>My Groups</h2><hr /><?php echo $mgList; ?><h2>All Groups</h2><hr /><?php echo $agList; ?></div></div>';
</script>

Please how do I fix this.

share|improve this question
    
What is groupModule and you also forgot closing brace. –  kimbarcelona Apr 23 at 2:37
    
@kimbarcelona its a module for my group thats meant to me dynamical generated. Its in the html after the javascript <div id="groupModule"></div> –  niceyy Apr 23 at 2:54
    
Please show the output of the PHP script as well. There isn't anything wrong in syntax except showGroups() closing curly bracket. –  josephting Apr 23 at 3:17
    
@josephting they are array that are meant to have these outputs $agList = '<a href="group.php?g='.$row["name"].'"><img src="groups/'.$row["name"].'/'.$row["logo"].'" alt="'.$row["name"].'" title="'.$row["name"].'" width="50" height="50" border="0" /></a>'; $mgList = '<a href="group.php?g='.$row["gname"].'"><img src="groups/'.$row["gname"].'/'.$row["logo"].'" alt="'.$row["gname"].'" title="'.$row["gname"].'" width="50" height="50" border="0" /></a>'; –  niceyy Apr 23 at 3:30
    
Looking at this, it is a string, not an array. Note that echo doesn't work on an array. You can merge an array of strings into 1 long string so that you can echo them. And it's just unclear when you say it's not working. What is the cause of making it not work? The info you posted here isn't enough to judge that. –  josephting Apr 23 at 3:52

4 Answers 4

up vote 0 down vote accepted

Overall, there isn't anything wrong with your code. However, it might have been caused by minor and hidden syntax error in your code.

If you use PHP script to generate JavaScript, check the output source code and make sure there isn't anything wrong with it.

Should the JavaScript not execute, debug it with your browser's console.

Uncaught ReferenceError: showGroups not defined

This means exactly what it says. showGroups is not defined. Make sure you have showGroups properly defined.

function showGroups() {
        _('groupModule').innerHTML = '<div id="groupWrapper"><div id="groupList"><h2>My Groups</h2><hr /><?php echo $mgList; ?><h2>All Groups</h2><hr /><?php echo $agList; ?></div></div>';
}
</script>

The closing curly bracket } is very important!

Uncaught SyntaxError:Unexpeted tokenError illegal

This is caused by invalid character in your code. Did you copy your code from elsewhere? Check if there's any special character found in your code. If so, remove it.

Source

According to the source code you included, there is a newline wrapped in assignment of $mglist and $aglist which caused JavaScript to act weirdly.

Put them in 1 line.

$agList .= '<a href="group.php?g='.$row["name"].'"><img src="groups/'.$row["name"].'/'.$row["logo"].'" alt="'.$row["name"].'" title="'.$row["name"].'" width="50" height="50" border="0"></a>';

$mgList .= '<a href="group.php?g='.$row["gname"].'"><img src="groups/'.$row["gname"].'/'.$row["logo"].'" alt="'.$row["gname"].'" title="'.$row["gname"].'" width="50" height="50" border="0"></a>';

Shared source code

share|improve this answer
    
I followed webinterect 2.0 tutorials [link]youtube.com/watch?v=d_PIyFRfHp and how do i get rid of these hidden invalid charaters. Am currently going through the codes again. –  niceyy Apr 23 at 5:27
    
I can't view the video. It varies depending on your editor. On Notepad++, you can show all symbols with View -> Show Symbols -> Show All Characters. Did you get the undefined ReferenceError fixed? –  josephting Apr 23 at 5:33
    
not yet I haven't please can I mail you the code instead.. this thing is getting me fraustrated –  niceyy Apr 23 at 6:01
    
Sure. Post it somewhere and link it. –  josephting Apr 23 at 6:33
    
snk.to/f-ctnlr02f thanks a lot –  niceyy Apr 23 at 6:51

This depends on what the php variable is, if it is scalar value, you could echo it directly (but remember to escape the special chars which will break the javascript syntax).

And if the php variable is not scalar value (eg: array), then you can't echo it directly, you have to construct the valid javascript code use the variable.

share|improve this answer
    
its an array that contains this $agList = '<a href="group.php?g='.$row["name"].'"><img src="groups/'.$row["name"].'/'.$row["logo"].'" alt="'.$row["name"].'" title="'.$row["name"].'" width="50" height="50" border="0" /></a>'; –  niceyy Apr 23 at 2:45
    
please how do I go about it –  niceyy Apr 23 at 3:03
<script type="text/javascript">
function showGroups() {
   document.getElementById('groupModule').innerHTML = '<div id="groupWrapper"><div id="groupList"><h2>My Groups</h2><hr /><?php echo $mgList; ?><h2>All Groups</h2><hr /><?php echo $agList; ?></div></div>';
}
</script>
share|improve this answer
    
Tried it but its till did not echo out @Devs –  niceyy Apr 23 at 3:00
    
You can try <?= $mgList; ?> instead. Also are you using php or html? –  Jamie Apr 23 at 3:06
    
@Jamie am using both they work hand in hand remember? Still doesn't work –  niceyy Apr 23 at 3:13
    
make sure the variables $mgList and $agList are not null –  Devs Apr 23 at 3:15
    
and, are they defined? are they in correct format? maybe trying to change ' to " and " to ' may help, maybe because of php ' " different. –  Jamie Apr 23 at 3:24
<?php
$mgList = 'Test $mgList ';
$agList = 'Test $agList ';
?>

<a href="#"><img src="sample.png" alt="groups" border="0" title="Groups" onclick="showGroups()"></a>
<div id="groupModule">-- groupModule --</div>

<script type="text/javascript">
function showGroups() {
    document.getElementById('groupModule').innerHTML = '<div id="groupWrapper"><div id="groupList"><h2>My Groups</h2><hr /><?php echo $mgList; ?><h2>All Groups</h2><hr /><?php echo $agList; ?></div></div>'; 
}
</script>

DEMO and click the image

If you are looking for something similar, let me know, i will upload the files

share|improve this answer
    
Yes i am looking for something similar. The $mgList and $agList are fetched from the database. Can I mail you the codes so you can understand what I mean. –  niceyy Apr 23 at 3:42
    
DOWNLOAD the files from here. You change the variables $mgList and $agList from the database. Please dont forgot to vote if you like the answer. –  Devs Apr 23 at 3:55
    
thanks, just found out that it actually echoes out but when the source code is viewed from the browser but its just not being displayed. –  niceyy Apr 23 at 4:29
    
just ran the browser console to check for errors and two errors were found. Uncaught ReferenceError: showGroups not defined and Uncaught SyntaxError:Unexpeted tokenError illegal –  niceyy Apr 23 at 4:55

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.