user730815
user730815

Reputation: 53

How to pass value JavaScript variable to PHP?

Passing value JavaScript variable to PHP.

Example

JavaScript

Var ddd='aaaa'

PHP

if (ddd="aaaa"){do somehting...}

Upvotes: 2

Views: 7104

Answers (4)

surge10
surge10

Reputation: 642

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... }

Upvotes: 0

Craig White
Craig White

Reputation: 14002

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.

Upvotes: 4

infinity
infinity

Reputation: 115

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;

Upvotes: -1

Danzan
Danzan

Reputation: 968

In JS:

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

In PHP

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

Upvotes: 0

Related Questions