1

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

2

5 Answers 5

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

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.

Comments

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.

5 Comments

So displaying HTML is also not possible, because HTML is client-side, and PHP server-side
@vladkras: No, The main duty of PHP is HTML Rendering and HTTP Handling, So PHP can manipulate html when it is rendering a page
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: 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?
I get what you're saying. I think @vladkras had a problem with how you said it. :)
-1

this should work

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

2 Comments

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];
You only can concatenate a javascript variable with a php variable, but in the other direction it is impossible.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.