简体   繁体   English

Ajax和jQuery无法将数据正确发送到php

[英]Ajax and jquery not sending data correctly to php

I created a basic form that uses jquery (ajax) to send data to php. 我创建了一个使用jquery(ajax)将数据发送到php的基本表单。 PHP should insert a new record based on the data to a mysql database. PHP应基于数据将新记录插入mysql数据库。 The reason for this is because I want to make insertions to the database without having to submit the whole form and then use the submit action for something else later. 这样做的原因是因为我想在不提交整个表单的情况下向数据库中插入数据,然后稍后再使用Submit操作。 It seems that the jquery works fine since the alert() shows the correct output for the variables, but the PHP does not insert the data and I don't get an error. 似乎jquery可以正常工作,因为alert()为变量显示了正确的输出,但是PHP没有插入数据,并且没有收到错误。 I can't figure out why this isn't working? 我不知道为什么这不起作用? I think it is a problem with my $post() because the function underneath does not execute but I can't pinpoint the error. 我认为$ post()有问题,因为下面的函数无法执行,但我无法查明错误。 Any help debugging this would be really appreciated. 任何调试它的帮助将不胜感激。 Or if anyone knows another way to get the same functionality that would be great too? 或者,如果有人知道获得相同功能的另一种方法,那就太好了吗? Thanks. 谢谢。 (The code below works fine now. I figured out it was a type cast error, and I fixed it. Hopefully someone can find this useful!) (下面的代码现在可以正常工作。我发现这是一个类型转换错误,我已将其修复。希望有人可以找到这个有用的方法!)

<script type="text/javascript">   
    function submitgrade(){
    alert("In it");
    var classID = $("#classSelect").val();
    var student = $("#studentSelect").val();
    var exam = $("#Exam").val();
    var grade = $("#grade").val();
    alert(classID+" - "+student+" - "+exam+" - "+grade);
    $.post('submitgrade.php',{postclassSelect:classID,poststudentSelect:student,postExam:exam,postgrade:grade}, /*1*/
    function(data){
    $("#grade").html("");
    });
    };            
</script>

       <?php       /*submitgrade.php*/


            $con=mysqli_connect("localhost","root","","studentbase");

            // Check connection
            if (mysqli_connect_errno())
            {
                echo "Failed to connect to MySQL: " . mysqli_connect_error();
            }
            $classID = $_POST['postclassSelect'];
            $studentID = $_POST['poststudentSelect'];
            $examID = $_POST['postExam'];
            $grade = $_POST['postgrade'];

            echo $studentID[0]." examID: ". $examID[0];
            $gradequery = "INSERT INTO grade VALUES(".intval($studentID).", '".$classID."', ".intval($examID).", ".intval($grade).");";


            $result = $con->query($gradequery);
            while($row = $result->fetch_assoc())
            {
                echo "<br /><p>Grade of ". $grade." submitted for exam ". $row['exam_id'] ." in ". $row['class_ID'] ."</p>";
            }
        ?>

Have you include this line in your html page ?? 您是否在HTML页面中加入了这一行?

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">

An example is here again, may help you 再次出现一个例子,可能对您有帮助

<script>
$(document).ready(function(){
 $("input").keyup(function(){
   txt=$("input").val();
   $.post("my_page.asp",{suggest:txt},function(result){
     $("span").html(result);
  });
 });
});

but your code seems correct too buddy !! 但您的代码似乎太正确了!

I suggest to continue debugging by attaching an error handler to your $.post call, your code could look this: 我建议通过将错误处理程序附加到$.post调用来继续调试,您的代码可能如下所示:

$.post('submitgrade.php', {postclassSelect:classID,poststudentSelect:student,postExam:exam,postgrade:grade})
.done(function(response) {
    // success
}).fail(function(response) {
    // failure
});

Further more you should check: 此外,您应该检查:

  • Is the script running on a server? 脚本是否在服务器上运行? ajax might not work on a file:/// address ajax可能不适用于file:///地址
  • Is the path from javascript location to php file correct? 从javascript位置到php文件的路径正确吗?
  • what do the browser developer tools say about the request that is initiated? 浏览器开发人员工具对已发起的请求怎么说?

I fixed it. 我修好了它。 It was actually just a syntax error in my SQL and a type difference error with one of my database columns. 实际上,这只是我的SQL中的语法错误和数据库列之一的类型差异错误。 The $grade variable is passed into PHP as a string. $ grade变量作为字符串传递到PHP中。 Once I wrapped all of my variables in intval() it worked as intended. 一旦将所有变量包装在intval()中,它就会按预期工作。 Stare at the code to long, sometimes you go blind. 盯着代码很长时间,有时您会视而不见。 Haha. 哈哈。

Thank you omnidan for the tip about sanitization. 感谢omnidan提供有关消毒的提示。 Here is a good guide that I used to apply it to my app: 这是我曾经将其应用于应用程序的一个很好的指导:

http://codex.wordpress.org/Validating_Sanitizing_and_Escaping_User_Data http://codex.wordpress.org/Validating_Sanitizing_and_Escaping_User_Data

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM