2

Passing value JavaScript variable to PHP.

Example

JavaScript

Var ddd='aaaa'

PHP

if (ddd="aaaa"){do somehting...}
3
  • 2
    What are you asking exactly? You could submit a form or make an ajax request... Commented May 23, 2011 at 3:36
  • I would do an ajax request here. Commented May 23, 2011 at 3:39
  • Could you pass it in the URL to the PHP code, an ajax post to domain.com/?ddd=aaaa A code example would be good! Commented May 23, 2011 at 3:48

4 Answers 4

4

Javascript is a Client side scripting language that is only executed after the page is fully loaded. PHP on the other hand is an on-demand compiling script. It parses the PHP within the file and outputs the resulting HTML to the user's Browser.

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

2 Comments

not strictly true; Javascript can be executed while the page is loaded, if there's a <script type='text/javacript'> block within the <body> tag for example. To ensure Javascript executes after page load, it would have to be in a onload handler.
True, as the document is downloaded and generated, it parses any script tags methodically inline with the rest of the html. This allows you do to do things like document.write('string');
0

In JS:

 $.post('ajax.php',{'dddd' : dddd}, function(data){
 });

In PHP

 if(isset($_POST['dddd'])) $dddd = $_POST['dddd'];

1 Comment

I really think you are not helping the OP by giving him a (jquery dependent) quick and dirty code without any explanation.
0

One way you can do that is to put it in the cookie:

var ddd = 'aaaa';
document.cookie = "ddd="+ddd; // the ddd in the "" is the name of the cookie

PHP

$ddd = $_COOKIE['ddd'];
if (ddd == "aaa") { do something... }

Comments

-1

I have tested the following code and it have worked. So please test this code:

function aa()
{
    var ddd='aaaa';
    window.location.href="newpage.php?Result=" +ddd;
}

click a button then will call aa() function and will go newpage.php where we get ddd variable value in Result variable

newpage.php:

extract($_GET);
echo $Result;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.