admin管理员组

文章数量:1418140

Passing value JavaScript variable to PHP.

Example

JavaScript

Var ddd='aaaa'

PHP

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

Passing value JavaScript variable to PHP.

Example

JavaScript

Var ddd='aaaa'

PHP

if (ddd="aaaa"){do somehting...}
Share Improve this question edited Jan 14, 2016 at 10:43 Adam Azad 11.3k5 gold badges31 silver badges72 bronze badges asked May 23, 2011 at 3:30 user730815user730815 532 silver badges6 bronze badges 3
  • 2 What are you asking exactly? You could submit a form or make an ajax request... – no.good.at.coding Commented May 23, 2011 at 3:36
  • I would do an ajax request here. – webdad3 Commented May 23, 2011 at 3:39
  • Could you pass it in the URL to the PHP code, an ajax post to domain./?ddd=aaaa A code example would be good! – addedlovely Commented May 23, 2011 at 3:48
Add a ment  | 

4 Answers 4

Reset to default 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 piling 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;

本文标签: How to pass value JavaScript variable to PHPStack Overflow