如何将PHP变量与JavaScript变量连接? 可能吗? [重复]

This question already has an answer here:

Java Script Code:

<script type="text/javascript">
        $(function () {
         var  seatNo = 2;                                                       
         str.push('<a title="' + seatNo + '">' + '<?php echo $thisPacket["seat"]; ?>'</a>');
         }); 
</script>

I want to concatenate between $thisPacket["seat"] with java Script variable seatNo.
Just like php concate. example: $i = 1; $thisPacket["seat".$i];

</div>

I want to concatenate between $thisPacket["seat"] with java Script variable seatNo. Just like php concate. example: $i = 1; $thisPacket["seat".$i];

No, this won't work because the PHP code runs on the server, and the javascript variable seatNo is not available until the javascript code executes on the client.

this should work

<script type="text/javascript">
        $(function () {
        var  seatNo = 2;                                                       
            str.push('<a title="' + seatNo + '"><?php echo $thisPacket["seat"]; ?></a>');
         }); 
</script>

No this is impossible, because JavaScript is a client-side language and will be executed after that all PHP commands was executed in the server and the page completely rendered. But PHP is a server-side language and is execued before any JavaScript code is interpreted.

take you php variable and assign it to javascript variable than concatenate them.

var phpVar = '<?php echo $thisPacket["seat"]; ?>';
var seatNo =  2;
var conVar = seatNo + phpVar;

I hope this will work

Your best bet is to serialize $thisPacket as a JSON object and send that to the client:

<script type="text/javascript">
        var thePacket = <?=json_encode($thisPacket);?>;
        $(function () {
        var  seatNo = 2;                                                       
            str.push('<a title="' + seatNo + '">' + thePacket['seat'+seatNo] + '</a>');
         }); 
</script>

But im guessing that you should really reconsider your current design.