我的jquery代码里面的PHP代码没有被执行[重复]

This question already has an answer here:

<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?

</div>

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.