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

This question already has an answer here:

If I have javascript something like this:

<javascript>
function myvalue()
{
    var a = 3;
}
</javascript>

How can I get the value of the variable a from the function. So, I can perform operation inside php code, something like this:

<?php 
$b = 1; 
$c = $b + a (variabel a from the function);
echo $c; // return 3
?>

Thank you very much

share|improve this question
If you have experience with JQuery this should be an easy task. If not, learning JQuery is easy :) api.jquery.com/jQuery.post – CoBolt Mar 18 at 7:38

marked as duplicate by Quentin, Mihai Iorga, cryptic ツ, nsgulliver, Emil Mar 18 at 9:23

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

4 Answers

up vote 4 down vote accepted

You can do this in two ways.

  1. Assign this value to a hidden input field and post it to that PHP page.
  2. Use AJAX to send to a PHP page.
share|improve this answer
It seems ill use #1. i'm don't have experience with ajax – Philips Tel Mar 18 at 8:11
@PhilipsTel Do some search about AJAX. If you feel it is so difficult, then go with first way. – Edwin Alex Mar 18 at 8:16
Thank you for advice Edwin.. I will – Philips Tel Mar 18 at 10:21
This answer has an unnecessary amount of up-votes relative the quality of its content. – RPM Mar 18 at 17:36

Here is a way to do this via AJAX

Step 1 Get the jQuery library from here: https://ajax.googleapis.com/ajax/libs/jquery/1.8/jquery.min.js

Step 2 The html page:

<html>
<head></head>
<body>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8/jquery.min.js"></script>
<script>
function myvalue() {
   var a = 3;
$.ajax({
  url: 'ajax_example.php'
  cache: false,
  type: 'GET',
  data: {var: a},
  dataType: 'JSON',
  beforeSend: function() { alert("About to deploy ajax request");
  success: function(response)
  {     
        console.log(response);
         if(response.success) {
               alert(response.var); 
         } else {
            alert(response.message); 
         }
  }
});
 $(document.ready(function() {
    myvalue();
 });
<script>

</body>
</html>

Step 3 The PHP page ajax_example.php

<?php
header("Cache-Control: no-cache, must-revalidate");
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT");
header('Content-Type: application/json; charset=utf-8');
$var = isset($_GET['var']) ? $_GET['var'] : FALSE;

if($var) 
{
   $var = htmlspecialchars($var, ENT_QUOTES, 'UTF-8');

   echo json_encode(array("success" => true, "var" => $var, "message" => "Example message"));
} else {

  echo json_encode(array("success" => false, "message" => "You need to provide a value"));
}
share|improve this answer

You should send the JS variable value through AJAX call to the PHP page. That's how the process works.

share|improve this answer
Would you mind to give an example? – Philips Tel Mar 18 at 10:23
You ajax function in jQuery is : $.ajax({type:"GET",url:"test.php",dataType:"html",data:{param : JS VAR}},success:function(data){ //data response is what you have echo from php file});...Now catch the JS var value in PHP file by $_GET["param"] and calculate in php and just echo "success" or "fail"...catch the response in ajax success method.. – Sudip Pal Mar 18 at 10:41

You have to use AJAX.

php runs in server, and javascript in cleint browser.

First code runs in server then it comes to browser so you can not pass js variables to php directly. you have to create a http request pass it to server using AJAX.

ref : http://api.jquery.com/jQuery.ajax/ for jquery ajax implementation.

share|improve this answer

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