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 CATIA有些零件打开直接单机确定终止
  • ¥15 请问有会的吗,用MATLAB做
  • ¥15 phython如何实现以下功能?查找同一用户名的消费金额合并—
  • ¥15 ARIMA模型时间序列预测用pathon解决
  • ¥15 孟德尔随机化怎样画共定位分析图
  • ¥18 模拟电路问题解答有偿速度
  • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对
  • ¥15 误删注册表文件致win10无法开启
  • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
  • ¥60 ESP32怎么烧录自启动程序,怎么查看客户esp32板子上程序及烧录地址