我有一个允许用户完成测验的网页。这些测验使用JavaScript在每次运行时填充原始问题。
免责声明:JS Noob警报。
问题完成后,用户将通过此功能获得最终分数:
function CheckFinished(){
var FB = '';
var AllDone = true;
for (var QNum=0; QNum<State.length; QNum++){
if (State[QNum] != null){
if (State[QNum][0] < 0){
AllDone = false;
}
}
}
if (AllDone == true){
//Report final score and submit if necessary
NewScore();
CalculateOverallScore();
CalculateGrade();
FB = YourScoreIs + ' ' + RealScore + '%. (' + Grade + ')';
if (ShowCorrectFirstTime == true){
var CFT = 0;
for (QNum=0; QNum<State.length; QNum++){
if (State[QNum] != null){
if (State[QNum][0] >= 1){
CFT++;
}
}
}
FB += '<br />' + CorrectFirstTime + ' ' + CFT + '/' + QsToShow;
}
这里的所有Javascript都是预先编码的,所以我正在努力破解它。然而,我正在努力研究如何通过PHP将变量RealScore传递给MySql数据库。
这里有关于stackoverflow的类似问题,但似乎没有人帮助我。
从外观上看,AJAX似乎得到了答案,但我如何将其实现到我的JS代码中呢?
RealScore只在测验完成后给出一个值,所以我的问题是如何将这个值发布到php,以及在完成测验后为我的数据库中的特定用户更新字段?< / p>
提前感谢您的帮助,如果您需要更多信息,请告诉我们!
答案 0 :(得分:1)
使用AJAX(不使用JQuery)存储数据
您尝试做的事情可能会带来一系列安全漏洞,如果您关心Web应用程序的安全性,请务必研究控制和捕获这些漏洞的方法。这些安全漏洞超出了本教程的范围。
<强>要求:强>
我们正在做的是编写两个脚本,一个在PHP
,一个在JavaScript
(JS)。 JS脚本将定义一个可用于动态调用PHP脚本的函数,然后根据它的响应做出反应。
PHP脚本只是尝试通过$_POST
将数据插入数据库。
要通过AJAX将数据发送到数据库,需要调用Ajax()函数,以下是函数的用法:
// JavaScript variable declarations
myUsername = "ReeceComo123";
myScriptLocation = "scripts/ajax.php";
myOutputLocation = getElementById("htmlObject");
// Call the function
Ajax(myOutputLocation, myScriptLocation, myUsername, RealScore);
所以,没有进一步的努力...
JavaScript文件:
/**
* outputLocation - any HTML object that can hold innerHTML (span, div, p)
* PHPScript - the URL of the PHP Ajax script
* username & score - the respective variables
*/
function Ajax(outputLocation, PHPScript, username, score) {
// Define AJAX Request
var ajaxReq = new XMLHttpRequest();
// Define how AJAX handles the response
ajaxReq.onreadystatechange=function(){
if (ajaxReq.readyState==4 && xml.status==200) {
// Send the response to the object outputLocation
document.getElementById(outputLocation).innerHTML = ajaxReq.responseText;
}
};
// Send Data to PHP script
ajaxReq.open("POST",PHPScript,true);
ajaxReq.setRequestHeader("Content-type","application/x-www-form-urlencoded");
ajaxReq.send("username="username);
ajaxReq.send("score="score);
}
PHP文件 (您需要填写MYSQL登录数据):
<?php
// MYSQL login data
DEFINE(MYSQL_host, 'localhost');
DEFINE(MYSQL_db, 'myDatabase');
DEFINE(MYSQL_user, 'mySQLuser');
DEFINE(MYSQL_pass, 'password123');
// If data in ajax request exists
if(isset($_POST["username"]) && isset($_POST["score"])) {
// Set data
$myUsername = $_POST["username"];
$myScore = intval($_POST["score"]);
} else
// Or else kill the script
die('Invalid AJAX request.');
// Set up the MySQL connection
$con = mysqli_connect(MYSQL_host,MYSQL_user,MYSQL_pass,MYSQL_db);
// Kill the page if no connection could be made
if (!$con) die('Could not connect: ' . mysqli_error($con));
// Prepare the SQL Query
$sql_query="INSERT INTO ".TABLE_NAME." (username, score)";
$sql_query.="VALUES ($myUsername, $myScore);";
// Run the Query
if(mysqli_query($con,$sql))
echo "Score Saved!"; // Return 0 if true
else
echo "Error Saving Score!"; // Return 1 if false
mysqli_close($con);
?>
答案 1 :(得分:0)
使用参数score
调用简单脚本。
"savescore.php?score=" + RealScore
在PHP中你保存它
$score = isset ($_GET['score']) ? (int)$_GET['score'] : 0;
$db->Query('INSERT INTO ... ' . $score . ' ...');
您可以通过Ajax或隐藏的iframe调用URL。
Ajax示例
var request = $.ajax({
url: "/savescore.php?score=" + RealScore,
type: "GET"
});
request.done(function(msg) {
alert("Save successfull");
});
request.fail(function(jqXHR, textStatus) {
alert("Error on Saving");
});
答案 2 :(得分:0)
我将这些函数用于没有JQuery的ajax,它只是一个javascript函数在IE6或更低版本中不起作用。使用正确的参数调用此函数,它应该可以工作。
//div = the div id where feedback will be displayed via echo.
//url = the location of your php script
//score = your score.
function Ajax(div, URL, score){
var xml = new XMLHttpRequest(); //sets xmlrequest
xml.onreadystatechange=function(){
if (xml.readyState==4 && xml.status==200){
document.getElementById(div).innerHTML=xml.responseText;//sets div
}
};
xml.open("POST",URL,true); //sets php url
xml.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xml.send("score="score); //sends data via post
}
//Your PHP-script needs this.
$score = $_POST["score"]; //obtains score from POST.
//save your score here
echo "score saved"; //this will be displayed in the div set for feedback.
所以使用正确的输入,div id,你的php脚本的url和分数来调用javascript函数。然后它会将数据发送到后端,您可以通过echo向用户发回一些反馈。