-8
<script>
$('#tes').click(function(){    
<?php $output = ''; 
  foreach( $votes as $array) 
    if($array->color === $colors->color)       
   $output = $output . $array->votes . '<br>';?>  
     $('#Result').html(<?php $output ?>);
   });
</script>

how should I rewrite it to make it work?

Nabin Kunwar
  • 1,965
  • 14
  • 29
markl17
  • 25
  • 5
  • 2
    Using PHP inside some JavaScript...and I thought mixing up PHP and HTML is the top of the things... – akluth Nov 22 '13 at 14:56
  • Well... That's a minus for coding style. You can't, just can't mix JavaScript and PHP, especially like that. Read about clean code, loose coupling, and how web works on the client and server sides. – deb0rian Nov 22 '13 at 14:57
  • PHP executes on the server, Javascript on the client, and never twain shall they meet except via AJAX calls. – Marc B Nov 22 '13 at 15:00
  • 1
    Guys, this is NOT a duplicate. This guy doesn't have an `echo` in front of `$output`. While the code is ugly, and warrants criticism, it's not at all the same issue as the other SO post marked. – jszobody Nov 22 '13 at 15:04
  • @jszobody OK, admittedly that *may* be the actual issue. But it seems just as likely to me that the dupe is appropriate. We don't know anything about what this code is *supposed* to do, so can't say for sure. Either way it's a lazy question. – deceze Nov 22 '13 at 15:06
  • @deceze So now we're marking dups just because we "can't say for sure"?? Lazy != duplicate. – jszobody Nov 22 '13 at 15:06
  • @jszobody Want to reopen it to close as "demonstrate some basic understanding"? :) – deceze Nov 22 '13 at 15:07
  • @deceze Ha, that might work. =) – jszobody Nov 22 '13 at 15:08

1 Answers1

0

Messy code issues aside, this:

<?php $output ?>

Should be:

<?php echo $output ?>

Also note that your JavaScript html() function will need a string, which means quotes around the HTML. Something like:

$('#Result').html("<?php $output ?>")

But then if your PHP $output has quotes in it, that'll break. So then you'll need to look at addslashes().

While this should fix your current issues, the commenters are right that this is a horribly messy/ugly way to write code, you need to refactor this significantly.

Suggestion: One way to make this a bit cleaner would be like this:

// At the top of your page
<?php
$output = '';
foreach( $votes as $array) {
    if($array->color === $colors->color) {
        $output .= $array->votes . '<br>';
    }
}
?>

// Down in your HTML code somewhere
<div id="output" style="display:none"><?php echo $output ?></div>

// Now for the much simpler javascript
<script>
    $('#tes').click(function(){
        $("#output").show();
    });
</script>

This way you have minimal mixing of PHP with HTML/JS, you don't have to worry about escaping quotes, the PHP $output is already on your page (hidden) ahead of time, and JavaScript just has to show it.

jszobody
  • 28,495
  • 6
  • 61
  • 72
  • Do not encourage that type of programming – deb0rian Nov 22 '13 at 14:58
  • @Fratyr It's still answering his specific question. which I thought was a GOOD thing to do here on SO! – jszobody Nov 22 '13 at 14:59
  • thank you for the comments i thought that one liner of php would not require whole ajax call besides how would php know which color was picked? little of noob style was simple hacking – markl17 Nov 22 '13 at 15:13
  • @user3022152 Did this answer solve your problem? – jszobody Nov 22 '13 at 15:14
  • any suggestions regarding actual code how to make it more amenable – markl17 Nov 22 '13 at 16:46
  • Separate out your PHP and HTML/JS code as much as possible. Build your `$output` at the top of the page first, in pure PHP. Then later perhaps do ``. Finally your javascript can then simply do `$("#output").show()`. Make sense? You have minimal php code mixed in with the HTML/JS. – jszobody Nov 22 '13 at 16:49
  • @user3022152 I updated my answer with a suggestion on how to refactor. I'm making a few assumptions about your page, you'd have to tweak it to work in your situation. – jszobody Nov 22 '13 at 16:54