admin管理员组文章数量:1384214
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.
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
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.
Thank You in advance!
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.
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
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.
Thank You in advance!
Share Improve this question asked Apr 22, 2015 at 19:43 YhlasYhlas 4213 gold badges5 silver badges17 bronze badges 10- Have you checked the console ? did you find any javascript errors ? – Nandan Bhat Commented Apr 22, 2015 at 20:02
- @Nandan yes, I checked and console does not show any errors. – Yhlas Commented Apr 22, 2015 at 20:08
- Are you getting any response ?? Simply echo (only echo) a message in php file and check whether you get response or not. – Nandan Bhat Commented Apr 22, 2015 at 20:12
- I am not getting any response. It looks like it is not running the php part. I checked the url to the files and they are correct. – Yhlas Commented Apr 22, 2015 at 20:15
- are you running on localhost ? I think you are trying to run HTML page which contains ajax call directly. – Nandan Bhat Commented Apr 22, 2015 at 20:17
2 Answers
Reset to default 0Just replaced ajax, by Jquery ajax,
Make sure all the URL variables are initialized before passing through url.
Change the $_GET method to $_POST in your PHP file.
Just Copy-Paste the solution.
<script src="http://ajax.googleapis./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
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'); */
?>
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();
本文标签: Insert record into database using javascriptMySQLand phpStack Overflow
版权声明:本文标题:Insert record into database using javascript, mysql, and php - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744527715a2610843.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论