Can you use javascript variable in php?

I'm trying to include JavaScript variables into PHP code as PHP variables, but I'm having problems doing so. When a button is clicked, the following function is called:

<script type="text/javascript">
    function addTraining(leve, name, date)
    {
        var level_var = document.getElementById(leve);
        var training_name_var = document.getElementById(name);
        var training_date_var = document.getElementById(date);

        <?php
            $result = "INSERT INTO training(level, school_name, training_date) VALUES('level_var', 'training_name_var', 'training_date_var')" or die("Query not possible.");
        ?>

</script>

Is it possible?

Can you use javascript variable in php?

CSᵠ

9,9799 gold badges39 silver badges63 bronze badges

asked Mar 4, 2010 at 12:33

2

PHP is run server-side. JavaScript is run client-side in the browser of the user requesting the page. By the time the JavaScript is executed, there is no access to PHP on the server whatsoever. Please read this article with details about client-side vs server-side coding.

What happens in a nutshell is this:

  • You click a link in your browser on your computer under your desk
  • The browser creates an HTTP request and sends it to a server on the Internet
  • The server checks if he can handle the request
  • If the request is for a PHP page, the PHP interpreter is started
  • The PHP interpreter will run all PHP code in the page you requested
  • The PHP interpreter will NOT run any JS code, because it has no clue about it
  • The server will send the page assembled by the interpreter back to your browser
  • Your browser will render the page and show it to you
  • JavaScript is executed on your computer

In your case, PHP will write the JS code into the page, so it can be executed when the page is rendered in your browser. By that time, the PHP part in your JS snippet does no longer exist. It was executed on the server already. It created a variable $result that contained a SQL query string. You didn't use it, so when the page is send back to your browser, it's gone. Have a look at the sourcecode when the page is rendered in your browser. You will see that there is nothing at the position you put the PHP code.

The only way to do what you are looking to do is either:

  • do a redirect to a PHP script or
  • do an AJAX call to a PHP script

with the values you want to be insert into the database.

answered Mar 4, 2010 at 12:36

Can you use javascript variable in php?

GordonGordon

308k72 gold badges527 silver badges551 bronze badges

0

<script type="text/javascript">
var jvalue = 'this is javascript value';

<?php $abc = "<script>document.write(jvalue)</script>"?>   
</script>
<?php echo  'php_'.$abc;?>

answered May 1, 2012 at 11:50

dg_0175dg_0175

3914 silver badges2 bronze badges

6

You seem to be confusing client-side and server side code. When the button is clicked you need to send (post, get) the variables to the server where the php can be executed. You can either submit the page or use an ajax call to submit just the data. -don

answered Mar 4, 2010 at 12:37

Don DickinsonDon Dickinson

6,1503 gold badges38 silver badges30 bronze badges

0

PHP runs on the server. It outputs some text (usually). This is then parsed by the client.

During and after the parsing on the client, JavaScript runs. At this stage it is too late for the PHP script to do anything.

If you want to get anything back to PHP you need to make a new HTTP request and include the data in it (either in the query string (GET data) or message body (POST data).

You can do this by:

  • Setting location (GET only)
  • Submitting a form (with the FormElement.submit() method)
  • Using the XMLHttpRequest object (the technique commonly known as Ajax). Various libraries do some of the heavy lifting for you here, e.g. YUI or jQuery.

Which ever option you choose, the PHP is essentially the same. Read from $_GET or $_POST, run your database code, then return some data to the client.

answered Mar 4, 2010 at 12:38

QuentinQuentin

876k121 gold badges1172 silver badges1287 bronze badges

I had the same problem a few weeks ago like yours; but I invented a brilliant solution for exchanging variables between PHP and JavaScript. It worked for me well:

  1. Create a hidden form on a HTML page

  2. Create a Textbox or Textarea in that hidden form

  3. After all of your code written in the script, store the final value of your variable in that textbox

  4. Use $_REQUEST['textbox name'] line in your PHP to gain access to value of your JavaScript variable.

I hope this trick works for you.

Can you use javascript variable in php?

answered Jul 11, 2010 at 7:28

SepehrSepehr

1,99517 silver badges29 bronze badges

2

You can take all values like this:

$abc = "<script>document.getElementByID('yourid').value</script>";

Can you use javascript variable in php?

Hugo Yates

2,0712 gold badges25 silver badges24 bronze badges

answered Mar 13, 2015 at 9:21

2

You can do what you want, but not like that. What you need to do is make an AJAX request from JavaScript back to the server where a separate PHP script can do the database operation.

answered Mar 4, 2010 at 12:40

dj_segfaultdj_segfault

11.8k4 gold badges28 silver badges35 bronze badges

Can you use a JavaScript variable in PHP?

The way to pass a JavaScript variable to PHP is through a request. This type of URL is only visible if we use the GET action, the POST action hides the information in the URL. Server Side(PHP): On the server side PHP page, we request for the data submitted by the form and display the result. $result = $_GET [ 'data' ];

Can JavaScript change PHP variable?

You can't change the value of a PHP variable in JavaScript. PHP runs on the server and JS runs on the client PC.

How use JavaScript variable on same page in PHP?

You can easily get the JavaScript variable value on the same page in PHP. Try the following codeL. <script> var res = "success"; </script> <? php echo "<script>document.

How use JavaScript variable in PHP mysql query?

php $var1 = $_POST['var1']; $var2 = $_POST['var2']; $getvalue="SELECT id,name from table1 WHERE column1='$var1' and column2='$var2'"; $result=mysql_query($getvalue) or die(mysql_error()); while($row=mysql_fetch_array($result)){ extract($row); echo $name; } ?>