javascript 使用 $.post 将 JS 变量发送到 PHP 脚本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15452802/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 00:48:38  来源:igfitidea点击:

Using $.post to send JS variables to PHP script

phpjavascript.post

提问by user2177629

I am trying to send Javascript variables over to a PHP script for updating scores of a quiz game. I've looked up the shorthand method of doing this, which is $.post, but I am unable to retrieve the values in the PHP script. I am fairly unfamiliar which JS and require help as to what I am doing wrong.

我正在尝试将 Javascript 变量发送到 PHP 脚本以更新测验游戏的分数。我查找了执行此操作的速记方法,即 $.post,但我无法检索 PHP 脚本中的值。我相当不熟悉哪个 JS 并且需要关于我做错了什么的帮助。

Here is the Javascript

这是Javascript

function updatescore(){
            var thisgamemarks= 2300;
            var thequizid = 5;
            $.post("updatemark.php", { quizidvalue: thequizid, newmarkvalue: thisgamemarks } );
            }

And the PHP

和 PHP

$studentnewmark = $POST['newmarkvalue'];
$thisquizid = $POST['quizidvalue'];
$thisstudentid = $_SESSION['studentid'];

回答by tay10r

type $_POST instead of $POST. also, on a windows based machine, you can use ctrl + shift + j to debug a js script - which will help if if there's any problems with your code. but, the js code you have shown us looks perfectly fine.

键入 $_POST 而不是 $POST。此外,在基于 Windows 的机器上,您可以使用 ctrl + shift + j 来调试 js 脚本 - 如果您的代码有任何问题,这将有所帮助。但是,您向我们展示的 js 代码看起来非常好。

回答by Touch

Yes. As suggest use the $_POST instead of $POST and try putting the "quizidvalue" in the javascript inside quotations. It might help. Just a thought. It's how I always do it.

是的。建议使用 $_POST 而不是 $POST 并尝试将“quizidvalue”放在 javascript 中的引号内。它可能会有所帮助。只是一个想法。这就是我总是这样做的方式。