开发者

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


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.


In JS:

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

In PHP

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


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


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;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜