dongyan6503 2015-04-22 19:43
浏览 49
已采纳

使用javascript,mysql和php将记录插入数据库

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!

  • 写回答

2条回答 默认 最新

  • dongqixian8474 2015-04-22 20:27
    关注

    Just 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.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
     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'); */
     ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 蓝牙耳机怎么查看日志
  • ¥15 Fluent齿轮搅油
  • ¥15 八爪鱼爬数据为什么自己停了
  • ¥15 交替优化波束形成和ris反射角使保密速率最大化
  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏