2014-09-20 82 views
0

我在使用PHP,Javascript和AJAX製作刪除系統時遇到了麻煩。我之前做過,但這次PHP和AJAX不會聯繫,Javascript腳本也不會做任何事情。PHP和AJAX不能一起工作

在Javascript文件中(server-log.js)警告某些東西不起作用,它不會來。這也不適用於AJAX/PHP處理程序文件(server-log.handler.php)。 控制檯錯誤沒有給出任何錯誤作爲回報,所有文件中的URL也是正確的。這是因爲我可以從Chrome中

JS腳本(服務器log.js)找到控制檯錯誤的每個文件:

$('.btn btn-xs btn-danger').click(function(){ 

    var press = $.this.attr('id'); 
    var press_url = 'action=delete&id='+press; 

     $.ajax({ 
      type:  'POST', 
      url:  'panel/includes/handlers/server-log.handler.php', 
      data:  press_url, 
      success: function(responseText) 
      { 
       $.parseJSON(responseText); 

        /* If and else controlling system */ 
        if(responseText.indexOf(1) > -1) 
        { 
         $("#danger").append('<strong>Helaas!</strong> Het is niet gelukt om de gevraagde log rij te verwijderen'); 
        } 
        else if(responseText.indexOf(2) > -1) 
        { 
         $("#danger").append('<strong>Helaas!</strong> Het opgegeven ID nummer is niet geldig'); 
        } 
        else if(reponseText.indexOf(100) > -1) 
        { 
         $("#success").append('<strong>Goed!</strong> De gevraagde log is succesvol verwijderd'); 
        } 
        else 
        { 
         $("#info").append('<strong>Info:</strong> Er is helaas een onbekende fout opgetreden <i>2255200914</i>'); 
        } 
      } 
     }); 
}); 

HTML部分(服務器log.php)

    <div class="alert alert-success" style="display: none;" id="success"></div> 
        <div class="alert alert-danger" style="display: none"; id="danger"></div> 
        <div class="alert alert-info" style="display: none;" id="info"></div> 
       <div class="error-log sscroll"> 
       <ul> 
       <form method="post"> 
       <?php 
       while($arrayLogs = $arrayqLogs->fetch_array()) 
       { 
       ?> 
        <li> 
        <span class="green">[<?php echo $arrayLogs['date']; ?>]</span> 
        <span class="red">[<?php echo $arrayLogs['action']; ?>]</span> 
        [<?php echo $arrayLogs['name']; ?>] <?php echo $arrayLogs['url']; ?> 
        <button class="btn btn-xs btn-danger" name="deleting" id="<?php echo $arrayLogs['id']; ?>"><i class="fa fa-times"></i></button> 
        </li> 
       <?php 
       } 
       ?> 
       </form> 
       </ul> 
       </div> 

PHP的處理程序部分(服務器log.handler.php)

<?php 
include_once '../config.php'; 

    if(isset($_POST['action']) && $_POST['action'] == 'delete') 
    { 
     $intAction  = trim($_POST['id']); 
     $error   = array(); 
     $bolean   = false; 

      # Prepared statement 
      $stmt = $mysqli->prepare("SELECT id,name,action,url,date FROM logs WHERE id = ?"); 
      $stmt->bind_param('i', $intAction); 
      $stmt->execute(); 
      $intStatement = $stmt->num_rows(); 
      $stmt->close; 

       /* Controlling part */ 
       if($intStatement == 0) 
       { 
        $error[] = 1; 
        $bolean  = true; 
       } 
       elseif(!ctype_digit($intAction)) 
       { 
        $error[] = 2; 
        $bolean  = true; 
       } 
        // Deleting and notifying 
        if($bolean == false) 
        { 
         $stmt = $mysqli->prepare("DELETE FROM logs WHERE id = ?"); 
         $stmt->bind_param('i', $intAction); 
         $stmt->execute(); 
         $stmt->close(); 

          # Returning error code 
          $error[] = 100; 
        } 

      /* Returning the value in a readable way */ 
      header('Content-Type: application/json'); 
      echo json_encode($error); 
    } 
?> 

所以這不是我第一次做這在我的網站,b我也沒有經常這樣做,所以我認爲我做了很多錯誤的事情。我非常感謝你們的幫助,英語不是我的母語,所以對於任何語法/拼寫錯誤的單詞都很抱歉。

+1

您是否發現'console.log()'停止響應的地步? – brasofilo 2014-09-20 21:29:48

+1

你爲什麼執行'SELECT'查詢,但從來沒有使用它返回的結果? – Barmar 2014-09-20 21:40:14

+0

@brasofilo如果我把它放在點擊函數之前,那麼我只能得到3個迴應中的1個... – RezaMO 2014-09-20 21:47:24

回答

2

這條線是不正確的:

var press = $.this.attr('id'); 

它應該是:

或:

var press = $(this).attr('id'); 

您還需要分配的$.parseJSON給一個變量的結果:

responseText = $.parseJSON(responseText); 

或者您可以在您的$.ajax()調用中指定dataType: 'json',jQuery將自動爲您解析響應。

+0

謝謝,那是一個錯誤。但它仍然不會有任何聯繫.. – RezaMO 2014-09-20 21:48:19