2

I want to add JavaScript variable to php echo

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

Thanks

3
  • 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? Commented Jun 1, 2012 at 16:00
  • 1
    Possible duplicate of Access a JavaScript variable from PHP. See also stackoverflow.com/questions/7016701/… Commented Jun 1, 2012 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... Commented Jun 1, 2012 at 16:03

1 Answer 1

6

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.

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

7 Comments

I hava php array and I want to convert it to javascript array Is this possible ?
var myJSArray = <?php echo json_encode($myPHPArray); ?>;
i'm sorry. but it can be. you need to echo all the script including your php variables and then run javascript
@MFarooqi — Your two sentences contradict each other.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.