0

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];

som
  • 4,650
  • 2
  • 21
  • 36
sami
  • 5
  • 2
  • 2
  • 5
  • possible duplicate of [how to put javascript variable in php echo](http://stackoverflow.com/questions/10853630/how-to-put-javascript-variable-in-php-echo) or [Access a JavaScript variable from PHP](http://stackoverflow.com/questions/2338942/access-a-javascript-variable-from-php) or of you meant otherwise echoing PHP into jS see [Pass a PHP string to a Javascript variable (and escape newlines)](http://stackoverflow.com/questions/168214/pass-a-php-string-to-a-javascript-variable-and-escape-newlines?rq=1) – metadings Jul 25 '13 at 06:35
  • This is a common misconception for novices because Javascript code and PHP code can co-exist in the same file. However, PHP executes _before_ HTML and Javascript is sent to the browser. Therefore you can't use Javascript variables in a PHP variable. You _can_ use the result of PHP variables in Javascript variables however. – Herbert Jul 25 '13 at 07:05

5 Answers5

1

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.

Hein Andre Grønnestad
  • 6,885
  • 2
  • 31
  • 43
1

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.

EJTH
  • 2,178
  • 1
  • 20
  • 25
0

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.

frogatto
  • 28,539
  • 11
  • 83
  • 129
  • 1
    So displaying HTML is also not possible, because HTML is client-side, and PHP server-side – vladkras Jul 25 '13 at 06:38
  • @vladkras: No, The main duty of PHP is `HTML Rendering` and `HTTP Handling`, So PHP can manipulate html when it is rendering a page – frogatto Jul 25 '13 at 06:42
  • the main duty is to be _general-purpose scripting language that is especially suited to web development_ you can render JS as well and even generate it on fly – vladkras Jul 25 '13 at 06:48
  • @vladkras: At this step you're right, But i think you did not read my answer carefully, My mean in this answer is **Access to content of JS variables is impossible while PHP is rendering**, Okay? – frogatto Jul 25 '13 at 06:52
  • I get what you're saying. I think @vladkras had a problem with _how_ you said it. :) – Herbert Jul 25 '13 at 07:07
0

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

-1

this should work

<script type="text/javascript">
        $(function () {
        var  seatNo = 2;                                                       
            str.push('<a title="' + seatNo + '"><?php echo $thisPacket["seat"]; ?></a>');
         }); 
</script>
freshp
  • 525
  • 5
  • 20
  • Yes, I know, but my problem is, I want to concatenate between $thisPacket["seat"] with java Script variable seatNo. Just like php concate. example: $i = 1; $thisPacket["seat".$i]; – sami Jul 25 '13 at 06:32
  • You only can concatenate a javascript variable with a php variable, but in the other direction it is impossible. – freshp Jul 25 '13 at 06:36