Insert record into database using javascript, mysql, and php -
i have following js function, makes ajax request, not doing reason. checked alerting url , displays supposed be, variables 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 code 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 system */ $query = mysqli_query($con, "insert 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 have ajax call(works perfect) in same page, think reason of problem. variables xmlhttprequest named different well.
thank in advance!
just replaced ajax, jquery ajax,
make sure url variables initialized before passing through url.
change $_get method $_post in php file.
just copy-paste 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 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 system */ $query = mysqli_query($con, "insert 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'); */ ?>
Comments
Post a Comment