尝试通过AJAX发送数字,然后将它们添加到我的数据库中
Trying to send numbers through AJAX then add them in my database
我有JavaScript代码(jQuery和Ajax)、一个数据库和一个PHP页面,我的Ajax正在引用这些代码。
我在这里的主要目的是:
- 我正在努力为孩子们建立一个网站(学校/专业项目)来帮助他们学习数学
- 他们必须回答一个问题,例如23+10,有不同程度的困难等等……但这并不是最重要的
- 这些值是用PHP编写到
<div>
中的。我的目标是提取它们并将它们插入到我的数据库中 - 我不会在这里写无用的细节,但他们基本上必须写一个数学答案。然后答案被发送到我的数据库,用于统计(好答案的百分比等)。所以我使用AJAX和jQuery,代码如下:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
$(function(){
$("#button").on("click", function(){
$.ajax({
url: "myWebsiteName.com/receiveQuestion.php",
type: "post",
data: {
id_question: $("#id_question").html(), resultat_operation: $("#reponse").html()
},
success: function() {
alert('success')
},
error : function() {
alert('error')
},
})
var num1 = parseInt(document.getElementById("num1").innerHTML, 10);
var num2 = parseInt(document.getElementById("num2").innerHTML, 10);
var reponse = parseInt(document.getElementById("reponse").value, 10);
var signe = document.getElementById("signe").textContent;
...
(and there's a function verifying the results, etc...)
引用的PHP页面代码在这里:
<?php
require('connexion.php');
require('fonctions.php');
if(isEnfant()) {
if(isset($_POST['id_question']) && isset($_POST['result_operation'])) {
try {
$id_questionInt = (int) $_POST['id_question'];
$result_operationInt = (int) $_POST['result_operation'];
$req = $bdd->prepare('INSERT INTO details (player, id_question_details, result_operation) VALUES (:id_player, :id_question, :resultOpe)');
$req -> bindValue(':id_player', $_SESSION['login_c']); // login of the children once he is online.
$req -> bindValue(':id_question', $id_questionInt);
$req -> bindValue(':resultOpe', $result_operationInt);
$req -> execute();
}
catch(PDOException $e) {
die('Error : ' . $e->getMessage());
}
}
}
?>
我尝试过的:
- 对值进行强制转换,因为显然通过post方法将这些值视为字符串
- 正在显示值,但没有结果
我通过消息知道请求是成功的,但的数据不会插入我的数据库。
您可能需要将值与PDO的一个类型常量绑定。
$id_questionInt = intval($_POST['id_question']);
$result_operationInt = intval($_POST['result_operation']);
$login_c = $_SESSION['login_c'];
$req = $bdd->prepare('INSERT INTO details (player, id_question_details, result_operation) VALUES (:id_player, :id_question, :resultOpe)');
$req -> bindValue(':id_player', $login_c); // login of the children once he is online.
$req -> bindValue(':id_question', $id_questionInt, PDO::PARAM_INT);
$req -> bindValue(':resultOpe', $result_operationInt, PDO::PARAM_INT);
$req -> execute();
相关文章:
- 将输入字段中的文本提交到我的数据库,同时将其添加到我的列表中
- 使用javascript从数据库中添加表
- 使用ASP.NET将谷歌地图添加到项目中,并从数据库中检索位置
- 如何使用PHP将javascript变量添加到mysql数据库中
- 当我在重新启动cordova应用程序后尝试添加更多数据时,lokijs会丢失数据库和收集中的数据
- 在javascript中跳过周六和周日,并向mysql数据库中添加不重复的条目
- 在不使用循环的情况下,从一个数据库字符串值向javascript数组添加多个对象
- 将数据库中的超链接添加到查询结果
- 将行从数据库添加到表视图
- indexedDB创建数据库并添加内容执行失败'交易'在'IDBDatabase'
- 使用Ajax和JQuery从SQL数据库中添加和删除一行
- 如何将任意数量的图像动态添加到源字符串位于数据库中的 Web 窗体
- 单击按钮将值添加到数据库
- 预先构建的GUI,用于上传.csv并将数据添加到mysql数据库
- 如何将动态添加的文本框插入MySQL数据库
- 如何将数据库系统添加到WebGL应用程序中
- 无法使用asp.net mvc将数据添加到数据库中
- 为什么不刷新就提交表单而不添加数据库值
- 当使用 JavaScript 动态添加数据库时,如何使用 PHP 向数据库添加信息
- 在 for 循环中添加数据库检索到的数据