使用 Ajax 获取 javascript 变量的值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22120196/
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
Using Ajax to get value of javascript variable?
提问by user3369289
Long time stackoverflow user but first time poster! I recently made a very simple little browser based game, and when I say simple I cannot stress this enough.
很长时间的 stackoverflow 用户,但第一次发帖!我最近制作了一个非常简单的基于浏览器的小游戏,当我说简单时,我怎么强调都不为过。
Anyway, the entire game logic is in one little javascript file. In that file I have a variable called value, and it contains the players score.
无论如何,整个游戏逻辑都在一个小的 javascript 文件中。在该文件中,我有一个名为 value 的变量,它包含玩家得分。
The user at any given time during gameplay is allowed to click Submit, and this then uses a php script to access and write their username and hopefully soon their score to my database.
用户在游戏过程中的任何给定时间都可以单击提交,然后这将使用 php 脚本访问并写入他们的用户名,并希望很快将他们的分数写入我的数据库。
The problem I am having and I know quite a few people new to Ajax have had is, how do I send the value of the javascript variable value to my php page which handles sending the variables to my database?
我遇到的问题是,我知道很多 Ajax 新手都遇到过这样的问题:如何将 javascript 变量值的值发送到处理将变量发送到我的数据库的 php 页面?
I have read quite a bit on this and can't seem to get it working. So starting from scratch, assume I have a functioning javascript file with the value variable containing the score. How do I send the score to the php file using ajax? Where would I place the Ajax code?
我已经阅读了很多关于此的内容,但似乎无法使其正常工作。所以从头开始,假设我有一个功能正常的 javascript 文件,其中的 value 变量包含分数。如何使用ajax将分数发送到php文件?我应该在哪里放置 Ajax 代码?
Thank you stackoverflow! Also I already know that javascript resides on the client side and php runs on the server side, I just would like to know how to properly send the score to the php file, that is all! Thanks again stackoverflow!
谢谢你!此外,我已经知道 javascript 驻留在客户端,而 php 运行在服务器端,我只想知道如何正确地将分数发送到 php 文件,仅此而已!再次感谢stackoverflow!
EDIT: Because I seem to be getting down voted this is what I have but it does not work:
编辑:因为我似乎被否决了,这就是我所拥有的,但它不起作用:
My Ajax:
我的阿贾克斯:
function ajaxCall() {alert("To AJAX: the value is: " + value);
$.ajax
( {
type: "POST",
url: "handler.php",
data: {'value' : value},
success: function(response)
{ alert("The value was passed!")}
}
);};
ajaxCall();
ajax调用();
Part of handler.php:
handler.php 的一部分:
if(isset($_POST['value']))
{
$value = $_POST['value']."";
var_dump($value);
}
var_dump($_POST['value']);
采纳答案by prateekkathal
Simply use this,
简单地使用这个,
your ajax.js should contain this.
你的 ajax.js 应该包含这个。
$(document).ready(function() {
$("#submitscore").click(function(event){
var user = $('#username').val();
var value = document.getElementById('yourscore').innerHTML;
$.post(
"submitscore.php",
{ username: user, score: value }
);
});
});
Your other file should have a button with the id "submitscore"
您的其他文件应该有一个 ID 为“submitscore”的按钮
Hi, <input type="text" id="username" size="12"><br/>
Your Score is : <span id="yourscore">5000</span><br/>
<button id="submitscore">Submit Score</button>
And in that submit file, use this:
在那个提交文件中,使用这个:
if(isset($_REQUEST['score'])||isset($_REQUEST['username']))
{
$username = $_REQUEST['username'];
$value = $_REQUEST['score'];
var_dump($value);
//Your Insertion/Updation Databse Script
}
回答by Tobias Mühl
Define a function that sets a variable. Be sure to define the variable in advance, so that it can be accessed outside of the function
定义一个设置变量的函数。一定要提前定义好变量,这样才能在函数外访问
function ajaxCall() {
alert("To AJAX: the value is: " + value);
var response_value; // define here or you won't be able to access it outside of success: f()
$.ajax({
type: "POST",
url: "handler.php",
data: {'value' : value},
success: function(response) {
response_value = response;
}
}
);
}
Also please don't use a semicolon after function() {}
. Sublime Text does that, still the syntax is incorrect.
也请不要在 之后使用分号function() {}
。Sublime Text 做到了这一点,但语法仍然不正确。
Edit: I recommend you to change the PHP file to directly output a JSON file. Things to consider when doing this:
编辑:我建议您将 PHP 文件更改为直接输出 JSON 文件。执行此操作时需要考虑的事项:
- Set the content-type with
header('Content-Type: application/json; charset=utf-8');
- Generate a PHP array or a single variable (e.g.
$result_array
) - Encode it to utf-8 JSON using PHP:
$json_string = json_encode($result_array); $json_string = utf8_encode($json_string);
- Then simply
echo($json_string);
- Tell jQuery that you are using JSON: Add
dataType: json
to your $.ajax() config
- 设置内容类型
header('Content-Type: application/json; charset=utf-8');
- 生成一个 PHP 数组或单个变量(例如
$result_array
) - 使用 PHP 将其编码为 utf-8 JSON:
$json_string = json_encode($result_array); $json_string = utf8_encode($json_string);
- 然后简单地
echo($json_string);
- 告诉 jQuery 您正在使用 JSON:添加
dataType: json
到您的 $.ajax() 配置