2016-09-20 175 views
-1

我正在將記錄細節加載到允許用戶編輯的模式中。我試圖實現的是讓用戶更新模式中的記錄,然後通過AJAX/jQuery提交給MySQL表,但是,在按下「保存更改」按鈕後,什麼都不會發生。我檢查了JS Query並確認該按鈕已正確鏈接,並在直接尋址PHP更新腳本時設法更新數據庫。不知道爲什麼腳本拒絕啓動jquery + AJAX - 通過Modal更新MySQL記錄

模態:

<div id="output"></div> 

<!-- Modal MYMODAL --> 
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel"> 
    <div class="modal-dialog" role="document"> 
    <div class="modal-content"> 
     <div class="modal-header"> 
     <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button> 
     <h4 class="modal-title" id="myModalLabel">Edit Record</h4> 
     </div> 
     <div class="modal-body"> 

     <!-- ID No. --> 
     <div class="form-group"> 
      <label>ID No.:</label> 
      <input type="number" class="form-control" id="dataPID" name="dataPID" size="5" disabled /> 
     </div> 
     <!-- /.id number -->           

     <!-- Category --> 
     <div class="form-group"> 
      <label>Category:</label> 
      <input type="text" class="form-control" id="dataCat" name="dataCat" /> 
     </div> 
     <!-- /.category --> 

     <!-- Issue --> 
     <div class="form-group"> 
      <label>Issue:</label> 
      <input type="text" class="form-control" id="dataIssue" name="dataIssue" /> 
     </div> 
     <!-- /.issue --> 

     <!-- Department Responsible --> 
     <div class="form-group"> 
      <label>Department Responsible:</label> 
      <input type="text" class="form-control" id="dataDeptResp" name="dataDeptResp" /> 
     </div> 
     <!-- /.department responsible --> 

     <!-- Experience --> 
     <div class="form-group"> 
      <label>Experience:</label> 
      <input type="text" class="form-control" id="dataExp" name="dataExp" /> 
     </div> 
     <!-- /.experience --> 

     <!-- textarea --> 
     <div class="form-group"> 
      <label>Description:</label> 
      <textarea class="form-control" id="dataDesc" name="dataDesc" rows="3" ></textarea> 
     </div> 

     </div> 
     <div class="modal-footer"> 
     <button type="button" id="SaveChanges" name="SaveChanges" class="btn btn-primary">Save Changes</button> 
     <button type="button" id="DeleteRecord" name="DeleteRecord" class="btn btn-danger">Delete Record</button> 
     <button type="button" class="btn btn-default" data-dismiss="modal">Close</button> 
     </div> 
    </div> 
    </div> 
</div> 
<!-- /.Modal MYMODAL --> 

的Javascript:

$("#SaveChanges").click(function() { 
    $.ajax({ 
     type: "POST", 
     url: "plugins/MySQL/ajax_action.php", 
     data: { action:"update_mysqli",PID:$("#dataPID").val(), Category:$("#dataCat").val(), Issue:$("#dataIssue").val(), Department_Responsible:$("#dataDeptResp").val(), Experience:$("#dataExp").val(), Description:$("#dataDesc").val()}, //your form data to post goes here as a json object 
     dataType: "json", 
     contentType:"text",          

     success: function(data) { 
      $('#output').html(data); 
      drawVisualization(); 
     }, 
    }); 
}); 

ajax_action.php

<?php 

error_reporting(E_ALL); 
ini_set('display_errors', 1); 

if(isset($_POST['action']) && ($_POST['action']=='update_mysqli')) { 

    // include connection details 
    include 'connect_db.php'; 

    //Open a new connection to the MySQL server 
    $db = new mysqli($dbhost,$dbuser,$dbpass,$dbname); 

    //Output any connection error 
    if ($db->connect_error) { 
     die('Error : ('. $db->connect_errno .') '. $db->connect_error); 
    } 

    // get variables and sanitize 
    $pid = mysqli_real_escape_string($db,$_POST['PID']); 
    $cat = mysqli_real_escape_string($db,$_POST['Category']); 
    $issue = mysqli_real_escape_string($db,$_POST['Issue']); 
    $dept_resp = mysqli_real_escape_string($db,$_POST['Department_Responsible']); 
    $exp = mysqli_real_escape_string($db,$_POST['Experience']); 
    $desc = mysqli_real_escape_string($db,$_POST['Description']); 

    // check if record exists based on ID number  
    $result = $db->query("SELECT * FROM qci_problems_index_new WHERE PID='".$pid."'"); 

    // if record is found, update accordingly 
    if ($result->num_rows > 0){ 

     $sql = "UPDATE qci_problems_index_new SET Category = '$cat', Issue = '$issue', Department_Responsible = '$dept_resp', Experience = '$exp', Description = '$desc' WHERE PID = '$pid'"; 

     if (!$db->query($sql)) { 
     echo "Error - Update of record PID " . $pid . " failed: (" . $db->errno . ") " . $db->error; 
     } 
    } else { 
     // if no record with relevant PID is found, create new record 
     $sql = "INSERT INTO `qci_problems_index_new`(`PID`, `Category`, `Issue`, `Department_Responsible`, `Experience`, `Description`) VALUES ('".$pid."', '".$cat."', '".$issue."', '".$dept_resp."', '".$exp."', '".$desc."')"; 

     if (!$db->query($sql)) { 
     echo "Error - could not insert new record: (" . $db->errno . ") " . $db->error; 
     } 

    } 

    echo "Success, record updated successfully"; 

    //close connection 
    $db->close(); 

} 

編輯1: 鉻控制檯說以下內容: XHR2 error

編輯2: 更新的代碼

+0

您是否檢查了控制檯的錯誤?檢查了PHP錯誤日誌中的錯誤?檢查網絡選項卡以查看是否已經發出實際呼叫,並且如果有,實際響應是什麼? –

+0

Btw ..你打開[SQL Injections](http://php.net/manual/en/security.database.sql-injection.php),並且應該使用[Prepared Statements](http:// php。 net/manual/en/mysqli.quickstart.prepared-statements.php)而不是串聯你的查詢。 'mysqli_real_escape_string()'不像人們想象的那麼安全...... –

+0

我沒有檢查,我的日誌沒有顯示任何東西。該請求會通過並直接尋址PHP腳本時起作用。 – Armitage2k

回答

1

更改您的數據類型爲JSON和內容類型爲文本,你的GET變量添加到POST請求

$("#SaveChanges").click(function() { 
    $.ajax({ 
     type: "POST", 
     url: "plugins/MySQL/ajax_action.php", 
     data: { action:"update_mysqli",PID:$("#dataPID").val(), Category:$("#dataCat").val(), Issue:$("#dataIssue").val(), Department_Responsible:$("#dataDeptResp").val(), Experience:$("#dataExp").val(), Description:$("#dataDesc").val()}, //your form data to post goes here as a json object 
     dataType: "json", 
     contentType:"text", 

     success: function(data) { 
      $('#output').html(data); 
      drawVisualization(); 
     }, 
    }); 
}); 

PHP

if(isset($_POST['action']) && ($_POST['action']=='update_mysqli')) { 
+0

我曾懷疑過,但即使進行了上述調整,它也不起作用。我刪除了禁用的PID輸入字段,因爲我認爲它可能會導致$ _POST的麻煩,但也沒有結果。 – Armitage2k

+0

你從你的xhr請求中得到了什麼狀態代碼? – madalinivascu

+0

這應該不重要。您可以發送一個查詢字符串發佈請求... –

0

您在「acti」中傳遞值「update_mysql」在「參數在ajax URL(插件/ MySQL/ajax_action.php? action = update_mysql)。在另一方面你在ajax_action.php情況只會被執行的代碼,如果「action」參數的值是「update_mysqli

更改以下行

if(isset($_GET['action']) && ($_GET['action']=='update_mysqli')) 

if(isset($_GET['action']) && ($_GET['action']=='update_mysql')) 

在您的ajax_action.php文件中。

OR

或者,您也可以通過值update_mysqli,而不是update_mysql在Ajax調用你的行動參數。 由於您使用的是mysqli,因爲您在代碼中使用了mysqli函數,所以您會更喜歡這一點。