dst2017 2014-08-22 20:53
浏览 19
已采纳

在php中捕获js var

Whats the best method of capturing a javascript variable sent using POST to a php file ?

My line of code that posts the variable is given below

 xmlHttp.open("GET", "testAJAX.php?$phpvariable="+$jsvariable, true);

Cheers

  • 写回答

1条回答 默认 最新

  • doushenyu8228 2014-08-22 21:44
    关注

    I'll bet the problem is you've confused yourself with the $ sign.

    Change:

    xmlHttp.open("GET", "testAJAX.php?$phpvariable="+$jsvariable, true);
    

    To:

    xmlHttp.open("GET", "testAJAX.php?phpvariable="+jsvariable, true);
    

    Why?

    Because calling $_GET['$phpvariable'] (single quotes) would give you the value of the parameter, whereas calling $_GET["$phpvariable"] (double quotes) would give you nothing! The $phpvariable inside double quotes would be assumed to be a PHP variable rather than a parameter name, and it would attempt to use the contents of the PHP variable (which probably doesn't exist) as the parameter name. (A parameter sent over HTTP is not a PHP variable.)

    But if you don't include that $ in your request parameter name, then both single and double quotes will work: $_GET['phpvariable'] or $_GET["phpvariable"].

    Also, Javascript variables don't begin with $ (not normally, although they can): I almost didn't catch that mistake.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?