0

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.

xdazz
  • 158,678
  • 38
  • 247
  • 274
niceyy
  • 3
  • 4
  • What is groupModule and you also forgot closing brace. – kimbarcelona Apr 23 '14 at 02:37
  • @kimbarcelona its a module for my group thats meant to me dynamical generated. Its in the html after the javascript
    – niceyy Apr 23 '14 at 02: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 '14 at 03:17
  • @josephting they are array that are meant to have these outputs `$agList = ''.$row['; $mgList = ''.$row[';` – niceyy Apr 23 '14 at 03: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 '14 at 03:52
  • @josephting I agree its string. What i mean by its not working is that the function showgroups() doesnt get called but whenever i comment the php code out it works that is the function is called. – niceyy Apr 23 '14 at 03:58
  • Okay. Then you should post the source code with the `echo` after you load the page. The problem lies there. There might be error causing the JavaScript to not run at all. Or you can use your browser's console to find out if there's any error. – josephting Apr 23 '14 at 04:00
  • @josephting Just viewed the browser's page source and i see it echoes out but its not being displayed. `_('groupModule').innerHTML =

    My Groups


    Web_Programming_Class

    All Groups


    Web_Programming_Class
    `
    – niceyy Apr 23 '14 at 04:25
  • Did you remove the quotes or it wasn't there? There should be `'` before `
    ` and after the last `
    `
    – josephting Apr 23 '14 at 04:29
  • @josephting Yes I removed it – niceyy Apr 23 '14 at 04:31
  • Then there just isn't any problem with the JavaScript. I just simulated your scenario with [this code](https://gist.github.com/josephting/61b3601c45e0fb92bf7c) and there isn't any problem at all. It might be caused by other things. – josephting Apr 23 '14 at 04:41
  • @josephting 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 '14 at 04:55
  • @josephting the Syntax error comes after `'

    My Groups


    Web_Programming_Class
    – niceyy Apr 23 '14 at 05:03

4 Answers4

0

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.

xdazz
  • 158,678
  • 38
  • 247
  • 274
0
<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>
J.K
  • 1,382
  • 1
  • 11
  • 27
0
<?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

J.K
  • 1,382
  • 1
  • 11
  • 27
  • 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 '14 at 03:42
  • [DOWNLOAD](http://webplums.com/temp/demo_23233778/demo_23233778.rar) the files from here. You change the variables $mgList and $agList from the database. Please dont forgot to vote if you like the answer. – J.K Apr 23 '14 at 03: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 '14 at 04: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 '14 at 04:55
0

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

Community
  • 1
  • 1
josephting
  • 2,617
  • 2
  • 30
  • 36
  • I followed webinterect 2.0 tutorials [link]https://www.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 '14 at 05: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 '14 at 05:33
  • not yet I haven't please can I mail you the code instead.. this thing is getting me fraustrated – niceyy Apr 23 '14 at 06:01
  • Sure. Post it somewhere and link it. – josephting Apr 23 '14 at 06:33
  • Ok, I found the problem. On line 19 and 32, remove the \n or newline. Make everything in 1 line. – josephting Apr 23 '14 at 08:11