使用 javascript、mysql 和 php 将记录插入数据库
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29807346/
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
Insert record into database using javascript, mysql, and php
提问by Yhlas
I have the following js function, which makes an ajax request, but it is not doing it for some reason. I checked alerting url and it displays it as it supposed to be, so all variables are declared.
我有以下 js 函数,它发出一个 ajax 请求,但由于某种原因它没有这样做。我检查了警报 url 并按预期显示它,因此声明了所有变量。
var request = new XMLHttpRequest();
var url = "ajax_js/q_ajax.php?q="+ques+
"&ans="+ans+
"&a="+inp[0].value+
"&b="+inp[2].value+
"&c="+inp[4].value+
"&d="+inp[6].value+
"&cor="+checked+
"&def="+input+
"&q_n="+q_name+
"&c_id="+c_id;
request.onreadystatechange=function (){
if(request.readyState==4 && request.status==200){
alert(request.responseText);
}
request.open("GET", url, true);
request.send();
}
Here is the code from php file.
这是来自php文件的代码。
<?php
require("db_conx.php");
$q = $_GET['q'];
$ans = $_GET['ans'];
$a = $_GET['a'];
$b = $_GET['b'];
$c = $_GET['c'];
$d = $_GET['d'];
$cor = $_GET['cor'];
$def = $_GET['def'];
$q_n = $_GET['q_n'];
$c_id = $_GET['c_id'];
$q = mysqli_escape_string($con, $q);
$ans = mysqli_escape_string($con, $ans);
$a = mysqli_escape_string($con, $a);
$b = mysqli_escape_string($con, $b);
$c = mysqli_escape_string($con, $c);
$d = mysqli_escape_string($con, $d);
$cor = mysqli_escape_string($con, $cor);
$def = mysqli_escape_string($con, $def);
$q_n = mysqli_escape_string($con, $q_n);
$c_id = mysqli_escape_string($con, $c_id);
/* Modify id for the system */
$query = mysqli_query($con, "INSERT INTO course_quiz (course_id, quiz_name, question, des_answer, ChoiceA,
ChoiceB, ChoiceC, ChoiceD, correct, def)
VALUES ('$c_id', '$q_n', '$q', '$ans', '$a', '$b', '$c', '$d', '$cor', '$def')");
echo('Question has been saved');
/* header('Location: ../instr_home.php'); */
I also have an another ajax call(works perfect) in the same page, which I think the reason of the problem. Variables for XMLHttpRequest are named different as well.
我在同一页面中还有另一个 ajax 调用(完美运行),我认为这是问题的原因。XMLHttpRequest 的变量也被命名为不同的。
Thank You in advance!
先感谢您!
采纳答案by Nandan Bhat
Just replaced ajax, by Jquery ajax,
刚刚用 Jquery ajax 替换了 ajax,
Make sure all the URL variables are initialized before passing through url.
确保在传递 url 之前初始化所有 URL 变量。
Change the $_GET method to $_POST in your PHP file.
在 PHP 文件中将 $_GET 方法更改为 $_POST。
Just Copy-Paste the solution.
只需复制粘贴解决方案。
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript">
function ajax()
{
var urlString ="q="+ques+"&ans="+ans+"&a="+inp[0].value+"&b="+inp[2].value+"&c="+inp[4].value+"&d="+inp[6].value+"&cor="+checked+"&def="+input+"&q_n="+q_name+"&c_id="+c_id;
$.ajax
({
url: "ajax_js/q_ajax.php",
type : "POST",
cache : false,
data : urlString,
success: function(response)
{
alert(response);
}
});
}
</script>
In your PHP file,
在您的 PHP 文件中,
<?php
require("db_conx.php");
$q = $_POST['q'];
$ans = $_POST['ans'];
$a = $_POST['a'];
$b = $_POST['b'];
$c = $_POST['c'];
$d = $_POST['d'];
$cor = $_POST['cor'];
$def = $_POST['def'];
$q_n = $_POST['q_n'];
$c_id = $_POST['c_id'];
$q = mysqli_escape_string($con, $q);
$ans = mysqli_escape_string($con, $ans);
$a = mysqli_escape_string($con, $a);
$b = mysqli_escape_string($con, $b);
$c = mysqli_escape_string($con, $c);
$d = mysqli_escape_string($con, $d);
$cor = mysqli_escape_string($con, $cor);
$def = mysqli_escape_string($con, $def);
$q_n = mysqli_escape_string($con, $q_n);
$c_id = mysqli_escape_string($con, $c_id);
/* Modify id for the system */
$query = mysqli_query($con, "INSERT INTO course_quiz (course_id, quiz_name, question, des_answer, ChoiceA,
ChoiceB, ChoiceC, ChoiceD, correct, def)
VALUES ('$c_id', '$q_n', '$q', '$ans', '$a', '$b', '$c', '$d', '$cor', '$def')");
echo('Question has been saved');
/* header('Location: ../instr_home.php'); */
?>
回答by Silence Peace
Change you code with this and you find your error
用这个改变你的代码,你会发现你的错误
request.onreadystatechange=function (){
//if(request.readyState==4 && request.status==200){
alert(request.responseText);
//}
}
request.open("GET", url, false);
request.send();

![Javascript 带 [ngmodel] 的角形材料 2 日期选择器](/res/img/loading.gif)