-1

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

1
  • If you have experience with JQuery this should be an easy task. If not, learning JQuery is easy :) api.jquery.com/jQuery.post Commented Mar 18, 2013 at 7:38

4 Answers 4

5

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.
Sign up to request clarification or add additional context in comments.

4 Comments

It seems ill use #1. i'm don't have experience with ajax
@PhilipsTel Do some search about AJAX. If you feel it is so difficult, then go with first way.
Thank you for advice Edwin.. I will
This answer has an unnecessary amount of up-votes relative the quality of its content.
3

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"));
}

Comments

0

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

2 Comments

Would you mind to give an example?
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..
0

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.