2014-02-08 59 views
0

我創建了一個使用jQuery(ajax)發送數據到php的基本表單。 PHP應該將基於數據的新記錄插入到mysql數據庫中。原因是因爲我想插入數據庫,而不必提交整個表單,然後使用提交操作獲取其他內容。看起來jquery工作正常,因爲alert()顯示變量的正確輸出,但PHP不插入數據,我也沒有收到錯誤。我不明白爲什麼這不起作用?我認爲這是我的$ post()的問題,因爲下面的函數沒有執行,但我無法找出錯誤。任何幫助調試這將非常感激。或者,如果有人知道另一種獲得相同功能的方式,那也會很棒?謝謝。 (下面的代碼現在工作得很好。我想通了,這是一個類型轉換的錯誤,我固定它。希望有人能找到這個有用!)Ajax和jquery沒有正確地發送數據到php

<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>"; 
      } 
     ?> 
+0

你可以看看這裏:http://stackoverflow.com/questions/12686457/sending-values-of-a-form -with-ajax-via-post/12686509#12686509與jQuery ajax – zer02

+0

順便說一下,請確保清理通過POST獲得的數據。 – omnidan

+0

我修好了。這實際上只是我的SQL中的一個語法錯誤,以及與我的一個數據庫列的類型差異錯誤。 $ grade變量作爲字符串傳入PHP。一旦我將所有變量都包含在intval()中,它就會按預期工作。長時間盯着代碼,有時你會失明。哈哈。 – natethenerd

回答

0

你在HTML頁面這條線?

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

一個例子再次在這裏,可以幫助你

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

,但你的代碼似乎是正確的太哥們!

0

我建議附加一個錯誤處理您$.post呼籲繼續調試,你的代碼可能看起來這樣:

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

而且更應該檢查:

  • 是在運行該腳本服務器? ajax可能無法在文件上工作:///地址
  • 從javascript位置到php文件的路徑是否正確?
  • 瀏覽器開發人員工具對啓動的請求有何看法?
+0

我在wamp服務器上運行這個。我發現錯誤是一種類型轉換。我不得不將我的變量包裹在intval()中,以使它們正確插入到數據庫中。即使如此,如果我有一個好的調試器,這將會很容易被捕獲。什麼是在服務器上調試PHP的最佳方法?我已經打開錯誤日誌記錄,我仍然看不到輸出? – natethenerd

0

我修好了。這實際上只是我的SQL中的一個語法錯誤,以及與我的一個數據庫列的類型差異錯誤。 $ grade變量作爲字符串傳入PHP。一旦我將所有變量都包含在intval()中,它就會按預期工作。長時間盯着代碼,有時你會失明。哈哈。

謝謝omnidan關於衛生處理的提示。下面是我用它應用到我的應用程序具有很好的指導:

http://codex.wordpress.org/Validating_Sanitizing_and_Escaping_User_Data

相關問題