-1

Hello, I have problem with my JS function. Please help me, I need this code until today and I can't understand what is the wrong :(

  foreach($group as $i => $name){
   print'<li onclick="groups(\'group'.$i.'\');" >'.$name.'</li>';
}

Then I have this code:

$moretags = "<script>";
$moretags .= 'function groups(grid){';
  foreach($group as $i => $name){
  $moretags .= '
  if(grid == \'group'.$i.'\'){
  document.getElementById(\'show\').innerHTML = \'group'.$i.'\';
  }
  ';

}


$moretags .= '</script>';
nbanic
  • 1,270
  • 1
  • 8
  • 11
Andrey Tsarev
  • 769
  • 1
  • 8
  • 25
  • 5
    You really should not create JavaScript with PHP. It's a nightmare to maintain and error prone, as you already noticed. Instead you should learn about the DOM and JavaScript and how they work together. The JS function is unnecessarily complex and is equivalent to: `function groups(grid) { document.getElementById('show').innerHTML = grid; };`. Yours does not work btw because you are missing a `}` to close the function. – Felix Kling Sep 30 '12 at 18:56
  • 1
    You need to specify what error you are getting, what should the output look like and what does it currently look like? – Suhail Patel Sep 30 '12 at 18:56
  • Adding to @FelixKling, maybe an easier way to manage this is to have a defined function that you don't generate with PHP, then let PHP generate an array in Javascript with each item in `$group`. The Javascript function can check if the `jList.indexOf(grid) > -1`, where `jList` is the array you generated. – Ian Sep 30 '12 at 19:04
  • I agree with all the above, but it also looks like you're missing a closing `}` for the end if the fubction – freefaller Sep 30 '12 at 19:08
  • Please explain what is your desired outcome then me or others might be able to help you achieve it. You just posted some broken code but you didn't say what do you exactly want to do. It would be best to include a [fiddle](http://jsfiddle.net/) or [jsbin](http://jsbin.com/). – rsp Sep 30 '12 at 19:30

2 Answers2

1

The proper way to do this would be to make an array or object in PHP with all of the data you need to pass to JavaScript on the client side.

$groups = array('some', 'elements', 'here');

Then, output it to your page:

<script>
    var groups = <?php echo json_encode($groups); ?>;
</script>

Then, you have full access to the data in JavaScript without having to dynamically generate the script as you are now. The only part you have to generate is the assignment of variables.

Brad
  • 159,648
  • 54
  • 349
  • 530
1

I completely agree with Felix Kling that you shouldn't generate JavaScript code with PHP code unless you really know what you're doing, which you apparently do not.

It is impossible to solve your problem because of the lack of details in your question but I'll try to at least help you make your code less error prone.

All you should generate in your PHP should be the HTML tags (at most!) without any onclick attributes, only classes and maybe some ids. Then just add the relevant event handlers in JavaScript (and use some framework like jQuery - read this to see why) or you can even add the HTML elements in JavaScript if they can't be used without JavaScript anyway.

Code generation is a hard thing. Generating JavaScript in PHP won't get you very far.

Community
  • 1
  • 1
rsp
  • 107,747
  • 29
  • 201
  • 177
  • I need dynamic code so i need javascript.I really hate using javascript but i think i cant cant make dynamic thing with js.Also im newbie at javascript and i dont know how to work with jquery – Andrey Tsarev Sep 30 '12 at 19:16
  • @AndreyTsarev if you're a JavaScript newbie then I will recommend jQuery even more. Using jQuery is much easier than *not* using jQuery. – rsp Sep 30 '12 at 19:23
  • thanks i am going to try jquery library – Andrey Tsarev Oct 03 '12 at 19:17