将Javascript信息发送到php并保存在数据库中
Send Javascript info to php and save it on a database
一位前同事开发了一个将结果发送到数据库的测验。他被解雇了,但我需要再次使用那个密码。我只有javascript代码,我需要重新创建php (save.php),从javascript保存信息。你能帮我的php代码或给我一个提示。Thaks !
$(document).ready(function() {
$("#answer_a").click(function() {
$.get("http://nameOfWebsite/save.php", {test: "1", question: "1", answer: "a" } );
});
$("#answer_b").click(function() {
$.get("http://nameOfWebsite/save.php", {test: "1", question: "1", answer: "b" } );
});
$("#answer_c").click(function() {
$.get("http://nameOfWebsite/save.php", {test: "1", question: "1", answer: "c" } );
});
$("#answer_d").click(function() {
$.get("http://nameOfWebsite/save.php", {test: "1", question: "1", answer: "d" } );
});
});
在save.php中使用$_GET[]来使用变量并将它们保存在您的表中
<?php
$con=mysqli_connect("example.com","peter","abc123","my_db");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// escape variables for security
$test = mysqli_real_escape_string($con, $_GET['test']);
$question = mysqli_real_escape_string($con, $_GET['question']);
$answer = mysqli_real_escape_string($con, $_GET['answer']);
$sql="INSERT INTO Persons (test, question, answer)
VALUES ('$test', '$question', '$answer')";
if (!mysqli_query($con,$sql)) {
die('Error: ' . mysqli_error($con));
}
echo "1 record added";
mysqli_close($con);
?>
使用$_GET[<name>]
获取php中的值,使用mysqli_connect
将数据插入表
您已经在正确的轨道上,下一步是在PHP中。您可以使用这个示例来获取这些值。考虑这个例子:
<?php
if(isset($_GET['test'])) {
$data = array(); // initialize return data holder
$test = isset($_GET['test']) ? $_GET['test'] : null;
$question = isset($_GET['question']) ? $_GET['question'] : null;
$answer = isset($_GET['answer']) ? $_GET['answer'] : null;
// they should be inside now, now you can go on with mysql inserts
// just a sample callback value to check if indeed php got it
$data['test'] = $test;
$data['question'] = $question;
$data['answer'] = $test;
echo json_encode($data);
exit;
}
?>
<!-- lets say this is an image -->
<button id="answerswer_a" type="button">Hi im an image</button>
<script src="jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#answer_a").click(function() {
$.get("index.php", {test: "1", question: "1", answer: "a" }, function(response){
var data = $.parseJSON(response);
console.log(data); // check this in console
});
});
});
</script>
相关文章:
- Javascript-如何读取json文件中的列并将其保存在Javascript数组中
- 如果使用 lodash 将属性存在于另一个对象中,则向对象添加属性
- Jquery菜单操作不稳定,定位不正确,存在一般错误
- 如何将getJson的响应保存在全局变量中
- 按下按钮时保存cookie
- 如何使用 Angular JS 将数据保存在数据库中
- 如何使用Spring MVC将Facebook返回的响应数据保存在Java类中
- 子字符串/正则表达式以获取字符串中保存的 SRC 值
- 用于检查数组中是否存在元素的javascript自定义方法
- HTML5在画布中加载较小的图像并保存实际大小的图像
- 是否可以在浏览器中使用纯JavaScript保存音频流
- 如何将多个画布保存为一个图像
- 使用javascript/jquery从现有数组中创建一个新数组,该数组保存项目存在的次数
- jQuery localStorage 如果存在,请保存它
- 如何在移动到新的HTML页面后使用保存在Javascript文件中的保存变量
- 根据页面元素的存在更改文件路径以保存页面内容
- 如何比较保存在变量中的querystring参数,并将变量保存在cookie中,并在每页上进行比较
- 在Json解析和在数据库中保存值方面存在问题
- 将对象替换为保存前已经存在的对象
- 如何在Mongoose中保存之前检查数据库中是否存在嵌入文档