Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I want to add javascrept variable to php echo

arr[i]='<?php echo $winner[javascript variable]; ?>';

Thanks

share|improve this question
1  
I don't believe what you are asking is possible. Do you want some value calculated by JavaScript to be inserted into a PHP script? – dpk2442 Jun 1 '12 at 16:00
Possible duplicate of Access a JavaScript variable from PHP. See also stackoverflow.com/questions/7016701/… – Juhana Jun 1 '12 at 16:02
if anything, you can use ajax to send a javascript variable to the server, then return the rendered PHP. but that's probably not the workflow you're looking for, since i'm sure you're talking about rendering a page normally... – Ian Jun 1 '12 at 16:03

closed as not a real question by John Conde, nickb, Zuul, Juhana, Graviton Jun 2 '12 at 2:11

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, see the FAQ.

1 Answer

up vote 3 down vote accepted

You can't.

  1. PHP runs on the server and outputs some text.
  2. The text is sent to the browser
  3. The browser interprets the text as HTML / JavaScript / etc

The PHP has finished running by the time the JavaScript is executed.

If you want to pass data back you need to make a new HTTP request and run a PHP script from scratch.

share|improve this answer
I hava php array and I want to convert it to javascript array Is this possible ? – Osama Ahmad Jun 1 '12 at 16:03
1  
var myJSArray = <?php echo json_encode($myPHPArray); ?>; – Quentin Jun 1 '12 at 16:05

Not the answer you're looking for? Browse other questions tagged or ask your own question.