-2

I would like to convert my javascript variable to a PHP variable...
Right now I have situation like that, in the code below I have a variable e , but I want to use e in PHP like $e:

<script>

function test()
{
var e = document.getElementById("category_id_video").value;
alert(e);


}
</script>


<?php
    if(isset($_GET['e'])) { 

    echo $video_player_id = $_GET['e'];
    }

?> 
gdoron
  • 147,333
  • 58
  • 291
  • 367
Krish
  • 387
  • 2
  • 13

2 Answers2

7

You simply can't.

PHP runs on the server, javascript runs on the client.
You need to send the value to the server with ajax or something like that.

gdoron
  • 147,333
  • 58
  • 291
  • 367
  • can you explain it by coding , if u dont mine – Krish Mar 16 '13 at 20:27
  • @Krish, explain what exactly? PHP and javascript run on different platforms. if you won't "PHP" to know something from "javascript" javascript needs to send it with [ajax](http://en.wikipedia.org/wiki/Ajax_(programming)) [MDN tutorials](https://developer.mozilla.org/en-US/docs/AJAX) – gdoron Mar 16 '13 at 20:31
  • ya sure @gdoron , if u observe my code category_id_video is id for dropdownlist, in dropdown list i am using onchange function, by that i am calling test function, here what i need is by clicking the option in list i want the value in $video_player_id . so could you explain it now – Krish Mar 17 '13 at 03:54
  • I see why others want to close this, because it demonstrates a very limited understanding of PHP and Javascript, but I can see how people new to coding could easily wonder this. – Chris Moschini Mar 17 '13 at 21:22
1

As gdoron points out, this isn't exactly possible, so let me roughly explain how to do what you seem to want to do.

When someone visits your site for the first time, their machine (and their browser running on their machine) sends an HTTP request to your server. At that time, the PHP in the requested page runs on your server. The Javascript at that time does not run, it's just meaningless text like any other text or HTML or CSS on the page.

Once the PHP is done running, HTML, CSS and Javascript is sent to the browser - the PHP that ran on your server does not go with it.

Now, the browser receives all of this and parses it, and runs the Javascript.

Here's a terrible text drawing of this process:

Browser --> Server (PHP) --> Browser (HTML/CSS/JS)

So you can see that it's not possible for you to just convert a Javascript variable value to PHP, because they run at different times - the PHP on your page has already run and finished by the time the Javascript starts, and they run in different places.

So, if you have values in Javascript that you want to process with PHP, you first need to bundle them up into JSON, then send them over AJAX to the server, to a page that's listening for those values that reacts accordingly. You can see an example here:

using jquery $.ajax to call a PHP function

Hopefully that clears up the basic confusion so you know where to go next with your learning.

Community
  • 1
  • 1
Chris Moschini
  • 36,764
  • 19
  • 160
  • 190