How to pass value JavaScript variable to PHP? - Stack Overflow

Passing value JavaScript variable to PHP.ExampleJavaScriptVar ddd='aaaa'PHPif (ddd="aaaa

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;

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745278842a4620180.html

相关推荐

  • How to pass value JavaScript variable to PHP? - Stack Overflow

    Passing value JavaScript variable to PHP.ExampleJavaScriptVar ddd='aaaa'PHPif (ddd="aaaa

    3小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信